I have got a single list of items which has all the comments and reply’s to a post.
I would like to format it depending on comments and reply’s together by comparing CommentID to ReplyToCommentId.
here is what im using foreach loop to get result which i would like to replace with linq
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);
}
}
}
Is this possible in LINQ.
Thanks in advance.
Or
You can make it faster (
O(n)rather thanO(n2)) by replacing the nestedWherecalls with a pre-computedToLookup(r => r.ReplyToCommentId)