开发者

how can i explain this code?

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 t开发者_运维百科he 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).


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.


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


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.


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


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


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.


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜