What is the quickest way to find out which .net framework linq methods (e.g .IEnumerable linq methods) are implemented using deferred execution vs. which are not implemented using deferred execution.
While coding many times, I wonder if this one will be executed right way. The only way to find out is go to MSDN documentation to make sure. Would there be any quicker way, any directory, any list somewhere on the web, any cheat sheet, any other trick up your sleeve that you can share? If yes, please do so. This will help many linq noobs (like me) to make fewer mistakes. The only other option is to check documentation until one have used them enough to remember (which is hard for me, I tend not to remember "anything" which is documented somewhere and can be looked up :D).
Generally methods that return a sequence use deferred execution:
and methods that return a single object doesn't:
So, methods like
Where
,Select
,Take
,Skip
,GroupBy
andOrderBy
use deferred execution because they can, while methods likeFirst
,Single
,ToList
andToArray
don't because they can't.There are also two types of deferred execution. For example the
Select
method will only get one item at a time when it's asked to produce an item, while theOrderBy
method will have to consume the entire source when asked to return the first item. So, if you chain anOrderBy
after aSelect
, the execution will be deferred until you get the first item, but then theOrderBy
will ask theSelect
for all the items.