22

I don't know whether the term of above title is appropriate.

Just like a and b:

var list = Enumerable.Range(0, 100);

var a = from l in list
        where l % 2 == 0
        select l;
var b = list.Where(l => l % 2 == 0);

When should I use each of them? And any difference?

  • It's the same. First one is called LINQ expression, second - lambda expression – realnero Mar 28 '13 at 09:14
  • 2
    @realnero Actually, the question's title is correct. Query syntax expression vs method syntax expression. – J. Steen Mar 28 '13 at 09:16
  • 3
    To complement the answers below - if you use a tool such as [linqpad](http://www.linqpad.net/), you can input this code yourself and run it, and then look at the IL-compilated code. It'll be exactly the same code for both statements. =) – J. Steen Mar 28 '13 at 09:23
  • 1
    As an asside, note that you could make a class with the correct `Select`, `Where` etc. methods (but not an `IQueriable`) and it could be used with query syntax and the methods would get called. See here for an interesting example of doing that: http://bartdesmet.net/blogs/bart/archive/tags/Z3/default.aspx and a question I had about setting it up nicely here: http://stackoverflow.com/questions/15566240/creating-a-type-that-can-be-used-as-a-func-method – George Duckett Mar 28 '13 at 09:32

5 Answers5

22

None, Query expression compiles into Method expression.

Query Syntax and Method Syntax in LINQ (C#)

Because queries return an IEnumerable, you compose them in method syntax by chaining the method calls together. This is what the compiler does behind the scenes when you write queries by using query syntax

Also see: LINQ Query Expressions (C# Programming Guide)

At compile time, query expressions are converted to Standard Query Operator method calls according to the rules set forth in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax. However, in most cases query syntax is more readable and concise. For more information, see C# Language Specification and Standard Query Operators Overview.

Apart from that one place where I have found something that can't be done in Query expression is to get the index along with the item. For example you can do following in method syntax:

var result = list.Select((r,i) => new { value = r, index = i});

In query expression an external variable has to be defined to achieve this purpose. Here is a similar discussion with answer from Jon Skeet

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
6

No

There is no difference between them.

From Query Syntax and Method Syntax in LINQ

Because queries return an IEnumerable, you compose them in method syntax by chaining the method calls together. This is what the compiler does behind the scenes when you write queries by using query syntax. And because a query variable does not store the results of the query, you can modify it or use it as the basis for a new query at any time, even after it has been executed.

Also from LINQ Query Expressions

At compile time, query expressions are converted to Standard Query Operator method calls.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
3

Query expression is transformed into standard method calls by compiler.

var a = from l in list
        where l % 2 == 0
        select l;

var b = list.Where(l => l % 2 == 0);

These two are exactly the same in compiled code.

However, not all methods have a keyword associated in query expression syntax.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

There's no difference, it's a matter of personal preference. You can even mix the two styles if you want to.

The Linq keywords are translated into the method call syntax by the C# compiler.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
1

The difference, in fact, is no.
In fact, it is one and the same thing, except that the expression for the $a$ the compiler converts to an expression equivalent to expression for $b$.

Stefan Babos
  • 296
  • 4
  • 9