Skip to content

Instantly share code, notes, and snippets.

@motowilliams
Created October 23, 2011 20:42
Show Gist options
  • Save motowilliams/1307867 to your computer and use it in GitHub Desktop.
Save motowilliams/1307867 to your computer and use it in GitHub Desktop.
Stupid-Linq-Tricks-Record-Rollup
[TestMethod]
public void multiple_comment_rollup()
{
IEnumerable<RawRecords> rawRecords = GetSampleRows();
const string EXPECTED_COMMENT_42 = "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.";
const string EXPECTED_COMMENT_69 = "Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.";
IEnumerable<Comment> actualComments =
from ncr in rawRecords
group ncr by new { ncr.Id, ncr.Timestamp }
into g
select new Comment
{
CommentId = g.Key.Id,
CommentTimestamp = g.Key.Timestamp,
CommentText = string.Join("", g.OrderBy(x => x.Sequence).Select(x => x.Comment))
};
//If you have multiple asserts in your normal tests, well you know what will happen
Assert.AreEqual(EXPECTED_COMMENT_42, actualComments.Single(x => x.CommentId == 42).CommentText);
Assert.AreEqual(EXPECTED_COMMENT_69, actualComments.Single(x => x.CommentId == 69).CommentText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment