Performance comparison of two Linq calls

Suppose there are two queries in the memory list:

First request (using extension methods):

var temp = listX.Where(q => q.SomeProperty == someValue);

Second query:

var temp = from o in listX
              where o.SomeProperty == someValue
              select o;

      

Is there a performance difference between the two queries; and if so, why?

+3


source to share


3 answers


No, there is no difference. The compiler internally converts the second version to the first.

The C # spec (ยง7.6.12) states:



The C # language does not specify the semantics of executing an expression query. Rather, query expressions are translated into method calls that follow the query expression pattern (ยง7.16.3). In particular, the query expression is converted into a method call with the name Where

, Select

, SelectMany

, Join

, GroupJoin

, OrderBy

, OrderByDescending

, ThenBy

, ThenByDescending

, GroupBy

and Cast

. These methods are expected to have specific signatures and result types, as described in ยง7.16.3. These methods can be instances of the method being requested on the object, or extension methods that are external to the object, and they implement the actual execution of the request.

The translation from query expressions to method calls is the syntactic mapping that occurs before the binding or overloading of any type of permission has been performed. The translation is guaranteed to be syntactically correct, but not guaranteed to be semantically correct C # code. After translating an expression query, the resulting method calls are treated as regular method calls, and this can in turn catch errors, for example, if the methods do not exist, if the arguments are of the wrong type, or if the methods are generic and type inference fails.

+8


source


There is no difference. It will produce the same result at the same time. It is basically the same code with different syntax.



+4


source


Short question, short answer:

There is no difference. They are both the same, just written in different syntaxes.

See also the MSDN documentation for Request Syntax and Method Syntax .

+3


source







All Articles