Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: What does All() return if there is no element?

This is a very simple question but "All" is such a bad keyword to google lol.

I want to get all categories, where none of its products are updated, or don't have any products.

In other words, get all categories, where all of its products are not yet updated, including all categories that don't have any products yet.

Is this the right expression?

var categs = context.Categories.Where(c => c.Products.All(x => !x.Updated));
like image 911
Aximili Avatar asked May 10 '12 07:05

Aximili


1 Answers

It returns true. From the documentation (emphasis mine):

Return value
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.

(This is the logical conclusion too. All of the elements in the sequence do indeed pass the predicate, in the same way that all of my daughters are over 10 feet tall. The fact that I don't have any daughters doesn't change the truth of the statement :)

See my Edulinq blog post on Any and All for more details about how they work.

like image 126
Jon Skeet Avatar answered Oct 02 '22 18:10

Jon Skeet