Created
October 23, 2011 20:40
-
-
Save motowilliams/1307866 to your computer and use it in GitHub Desktop.
Stupid-Linq-Tricks-Record-Rollup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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