开发者

what is the difference between two query type in LINQ?

I have a table was named "MYTABLE". T开发者_如何学运维hat have two columns "FIRSTNAME" and "LASTNAME". Two query below returned same result is IQueryable<MYTABLE>

dataContext.MYTABLEs.Where(f => f.FIRSTNAME == firstName && f.LASTNAME == lastName);

from t in dataContext.MYTABLEs
where t.FIRSTNAME == firstName && t.LASTNAME == lastName select t;

What is the difference? which one in the two query faster?


They're both the same. You can write LINQ queries using lambda (method) syntax (the 1st approach) or query syntax (the 2nd approach). The latter is simply syntactic sugar and both get compiled to the same thing.

From the LINQ Query Syntax versus Method Syntax MSDN article:

there is no semantic difference between method syntax and query syntax. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls.

A similar question can be found here.


These compile to identical IL. The latter is syntactic sugar for the former (i.e., the compiler just translates the query syntax to the method syntax before outputting IL).

However, there are some slight differences between the two. There are some queries that can only be expressed in the method syntax (for example, data.Count(somePredicate) or data.Max(somePredicate)).

For additional discussion on the LINQ Query Syntax versus the Method Syntax, see MSDN.


They are exactly the same. They both compile to the same MSIL. One is just calling the methods on IQuerable and the other is using extensions added to C# to do the same thing.


They are the same and should compile down to the same code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜