Understanding how the C # compiler deals with binding linq methods

I am trying to wrap what the C # compiler does when I chain linq methods, especially when repeatedly connecting the same method.

A simple example: . Let's say I'm trying to filter out an int sequence based on two conditions.

The most obvious thing to do is something like this:

IEnumerable<int> Method1(IEnumerable<int> input)
{
    return input.Where(i => i % 3 == 0 && i % 5 == 0);
}

But we could also associate the where methods with one condition in each:

IEnumerable<int> Method2(IEnumerable<int> input)
{
    return input.Where(i => i % 3 == 0).Where(i => i % 5 == 0);
}

I looked at IL in Reflector; it is clearly different for the two methods, but its analysis is not further clear at the moment :)

:
a), - .
b) - ( -, !)

+5
2

(a) , :

- , ! , , - "Where clause" , LINQ to Objects. .

: , Where, WhereEnumerable, - IEnumerable (, Where on) , .

WhereEnumerable (, foreach ), IEnumerable, .

" foreach , ".

, , - . Enumerable ", ", , . a Where , , . , . , , .

, MoveNext false, , .

(b), , , . :)

+8
  • , . , , - .

  • .

+1

All Articles