Task SearchPostAsync(List <PostWithComment> postWithComments, IEnumerable <string> valueToSearch, DateTime?date, CancellationToken cancellationToken) { return(Task.Factory.StartNew(() => { foreach (var post in postWithComments) { cancellationToken.ThrowIfCancellationRequested(); if (SearchPost(valueToSearch, date, post.Post, searchedValue)) { continue; } var p = new PostWithComment { Post = post.Post }; SearchComment(valueToSearch, date, post.Comments, p); if (p.Comments.Count() > 0) { searchedValue.Add(p); } } })); }
void SearchComment(IEnumerable <string> valueToSearch, DateTime?date, IEnumerable <ItemComment> itemComment, PostWithComment post) { if (itemComment != null && itemComment.ToList().Count > 0) { itemComment.ToList().ForEach(i => { if (CheckDate(i.date, date)) { if (SearchValue(i.text, valueToSearch)) { var list = post.Comments.ToList(); list.Add(i); post.Comments = list; } } }); foreach (var item in itemComment) { SearchComment(valueToSearch, date, item.thread.items, post); } } }