4

I have a List<MyList> of objects.

MyList also has in it several Lists and one might be called List<Defect>.

List<Defect> can contain multiple defects one or more of which might be null.

How can I return a count of MyList items where MyList.Defects contains a null object?

I know I can do a foreach and check every item but is there a LINQ way to do this?

2 Answers 2

4

How can I return a count of MyList items where MyList.Defects contains a null object?

return myLists.Count(ml => ml.Defects.Contains(null));
Sign up to request clarification or add additional context in comments.

Comments

4
return myLists.Count(ml => ml.Defects.Any(d => d==null));

1 Comment

+1 Whilst I think this solution is probably easier to read, I like @mquander's solution a little better for its brevity. Thank you any way @Joel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.