
Untitled
By: a guest on
Jul 31st, 2012 | syntax:
None | size: 0.72 KB | hits: 13 | expires: Never
Linq to format a list of items to replace foreach loop
List<UserComment> comments = GetComments(SomeID).ToList();
int i = 0;
foreach(var item in comments) {
if(item.IsReply == false) {
i++;
formatedComments.Add(item);
foreach(var replys in comments) {
if(item.CommentID == replys.ReplyToCommentId) {
i++;
formatedComments.Add(replys);
}
}
}
from c in comments
where !c.IsReply
from r in new[] { c }.Concat(
comments.Where(r => c.CommentID == r.ReplyToCommentId)
)
select r
comments
.Where(c => !c.IsReply)
.SelectMany(c => new[] { c }.Concat(
comments.Where(r => c.CommentID == r.ReplyToCommentId)
)