The most important thing to realize is that, using Linq, the query does not get evaluated immediately. It is only run as part of iterating through the resulting IEnumerable<T>
in a foreach
- that's what all the weird delegates are doing.
So, the first example evaluates the query immediately by calling ToList
and putting the query results in a list.
The second example returns an IEnumerable<T>
that contains all the information needed to run the query later on.
In terms of performance, the answer is it depends. If you need the results to be evaluated at once (say, you're mutating the structures you're querying later on, or if you don't want the iteration over the IEnumerable<T>
to take a long time) use a list. Else use an IEnumerable<T>
. The default should be to use the on-demand evaluation in the second example, as that generally uses less memory, unless there is a specific reason to store the results in a list.