1

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 ?

1
  • All of above queries are returning same results if you write it manually. But compiler does not have an idea about what is inside the select method. so it will not optimise up to that level. Commented Apr 2, 2013 at 5:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.