tags:

views:

67

answers:

1

Hello

from item in range where item % 2 ==0 select i ;

is converting to lamda expressions as below

range.where(item % 2 ==0).select(x=>x).

I feel that first way of linq is translating next one and if it is ,so is there any optimization like this range.where(item & 2 == 0) instead of other one ?

+2  A: 

No the C# compiler will not ever remove the .Select call at the end of the LINQ query. The reason why is that the C# compiler has no knowledge of what the .Select method does and hence cannot remove it as an optimization.

The compiler cannot have this knowledge because it binds to Select in a very flexible way. It will consider any instance or extension method named Select on the target type which has the appropriate signature. You can even define your own Select methods to do customized actions like logging. If the C# compiler removed the Select clause in this case it would break this type of code.

JaredPar
all these queries which i wrote has same result ?
Freshblood
@Freshblood, I don't understand what you mean by that
JaredPar
Yes, these three query produce same result that was what i asked.
Freshblood