Aximili
Aximili

Reputation: 29444

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));

Upvotes: 12

Views: 3183

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1499840

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.

Upvotes: 19

lex87
lex87

Reputation: 1326

All "Determines whether all elements of a sequence satisfy a condition."

MSDN

I think that your expression is correct.You get all the categories which contain products that are not updated.

Upvotes: 4

Related Questions