I have the below code that uses yield to build a collection:
public IEnumerable<Comment> GetComments(
IEnumerable<ContentItem> commentContentItems)
{
foreach (var commentContentItem in commentContentItems)
{
var Comment = new Comment
{
CommentCreatedByName = commentContentItem.InitiatorName,
CommentCreatedByID = commentContentItem.CreatedByID,
ContentItemID = commentContentItem.ContentItemID,
CommentDate = commentContentItem.CreatedDate
};
yield return Comment;
}
}
I want to start checking if an item is deleted and, if so, yield in such a way as it won't add the deleted item to the collection.
I know I could use linq to reduce the set like this:
foreach (var commentContentItem in commentContentItems.Where(x => !x.Deleted))
But for arguments sake; how would you do this using yield for, let's say, situations where yield is more performant?
eg:
if (commentContentItem.Deleted)
{
yield return null;
}