Skip to content

Instantly share code, notes, and snippets.

@motowilliams
Created October 23, 2011 20:40
Show Gist options
  • Save motowilliams/1307866 to your computer and use it in GitHub Desktop.
Save motowilliams/1307866 to your computer and use it in GitHub Desktop.
Stupid-Linq-Tricks-Record-Rollup
//Source object where depending on the length of the text
public class RawRecords
{
public int Id { get; set; }
public int Sequence { get; set; }
public DateTime Timestamp { get; set; }
public string Comment { get; set; }
}
//Destination object where you want the multiple comment lines to be rolled up into the destination CommentText property.
public class Comment
{
public int CommentId { get; set; }
public DateTime CommentTimestamp { get; set; }
public string CommentText { get; set; }
}
[TestMethod]
public void single_comment_rollup()
{
IEnumerable<RawRecords> rawRecords = GetSampleRow();
const string EXPECTED = "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.";
var actualComment = new Comment
{
CommentId = rawRecords.First().Id,
CommentTimestamp = rawRecords.First().Timestamp,
CommentText = string.Join("", rawRecords.OrderBy(ord => ord.Sequence).Select(x => x.Comment))
};
Assert.AreEqual(EXPECTED, actualComment.CommentText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment