Reputation: 67223
in .net, it is possible to write:
(from n in numbers where n == 5 select n).ToList();
without those brackets, it is not possible to call the ToList() method. how can i explain to someone what this line does (all i can say is it precompiles the query, but i do not know if this is actually 100% correct).
Upvotes: 4
Views: 197
Reputation: 125488
That looks to me like a LINQ expression wrapped in parentheses, the result of which will be IEnumerable<int>
followed by a call to the IEnumerable<T>
extension method ToList<T>()
.
I think in this case, the call ToList<T>
forces the expression to be evaluated immediately, essentially foregoing any laziness to the evaluation.
Eric White wrote a good article on Lazy (and Eager) Evaluation with LINQ
Upvotes: 1
Reputation: 172270
from n in numbers where n = 5 select n
is actually syntactic sugar for numbers.Where(n => n == 5)
. So you are filtering the list of numbers to those which equal 5 using a LINQ expression.
LINQ, however, evaluates lazy. That means that the object returned by numbers.Where(n => n == 5)
(an IEnumerable) is not the list of numbers equaling five. The list is created only when needed, i.e., when trying to access the elements of the IEnumerable.
ToList
copies the contents of this IEnumerable into a list, which means that the expression has to be evaluated right now.
Upvotes: 6
Reputation: 57919
The parentheses group expressions into a logically atomic value.
This is analogous to arithmetic
1 + 1 * 2 = 4
NO! (= 3)
(1 + 1) * 2 = 4
YES!
Upvotes: 0
Reputation: 24453
You might find it easier to explain to someone not familiar with LINQ if you change it to use the extension methods directly.
numbers.Where(number => number == 5).ToList();
This becomes much more obvious for a simple case like this.
Upvotes: 1
Reputation: 218847
Someone more familiar with LINQ may correct me, but it's my understanding that the LINQ expression itself isn't necessarily evaluated until you "do something" with it. So if you want it evaluated right away then you'd call ToList() as such. (Which also does convert it from IEnumerable to IList, not just evaluate it.)
It's wrapped in the parentheses, of course, to tell the compiler the full extent of what's being ToList()'ed.
Upvotes: 0
Reputation: 57958
In LINQ, by default, all queries are lazy-loaded. That is, until your collection is enumerated, the query itself has been parsed but not run.
The ToList() forces enumeration over the collection "numbers", and the query is run.
Upvotes: 2
Reputation: 171411
The brackets are there to disambiguate for the parser that the .ToList()
could be referring to just n
.
Upvotes: 5