Linq Queries converting to lambda expressions ?
开发者_JAVA百科from item in range where item % 2 ==0 select i ;
extension methods equivelant of it is.
range.where(item % 2 ==0).select(x=>x)
.
I feel that first way of linq is translating next one by compiler and if it is,so is there any optimization by compiler like this range.where(item & 2 == 0)
instead of other one ?
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.
精彩评论