tags:

views:

157

answers:

8

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

+5  A: 

The brackets are there to disambiguate for the parser that the .ToList() could be referring to just n.

RedFilter
+1  A: 

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

Russ Cam
+2  A: 

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.

Peter J
Heinzi explains it more succinctly than I.
Peter J
But you were faster. ;-)
Heinzi
+1  A: 

It converts, from an IEnumerable<t> to a List<t>

Ben Robinson
A: 

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.

David
+6  A: 

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.

Heinzi
+1  A: 

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.

John Bowen
A: 

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!

Jay