Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does `Any()` forces linq execution?

I have a linq to entity query.

will Any() force linq execution (like ToList() does)?

like image 809
Elad Benda Avatar asked Dec 03 '22 00:12

Elad Benda


2 Answers

There is very good MSDN article Classification of Standard Query Operators by Manner of Execution which describes all standard operators of LINQ. As you can see from table Any is executed immediately (as all operators which return single value). You can always refer this table if you have doubts about manner of operator execution.

like image 193
Sergey Berezovskiy Avatar answered Dec 24 '22 15:12

Sergey Berezovskiy


Yes, and no. The any method will read items from the source right away, but it's not guaranteed to read all items.

The Any method will enumerate items from the source, but only as many as needed to determine the result.

Without any parameter, the Any method will only try to read the first item from the source.

With a parameter, the Any method will only read items from the source until it finds one that satisfies the condition. All items are only read from the source if no items satisfies the condition until the last item.

like image 45
Guffa Avatar answered Dec 24 '22 15:12

Guffa