ASP.NET Hosting

Query syntax vs. Method syntax

In the official LINQ forum, someone asked whether it's better to use query syntax (query expression) or method syntax (dot notation).

My answer is that it's mostly a matter of tastes and it depends on the query.
If I'm writing a query that uses many query operators that aren't supported by the query syntax, then I use the method syntax. Mixing the two syntaxes in one single query can quickly make it hard to read, with all the parentheses.
If I'm writing a query that consists of only one operation, then I use the method syntax as well.
In other cases, the query syntax has advantages, like the ability to use "range" variables. The ones defined in from clauses. If you use the method syntax, then you have to redeclare the variables in each lambda expression passed as a parameter to a query operator.

Let's consider the following example:

from person in persons
where person.Age > 12
orderby person.LastName
select person.LastName;

Here, person is declared once and used in the where, orderby, and select clauses.
If you write the same query using the method syntax, you have to "redeclare" person each time in the lambda expressions:

persons
  .Where(person => person.Age > 12)
  .OrderBy(person => person.LastName)
  .Select(person => person.LastName);

Another advantage of the query syntax is the ability to use let clauses. This is really useful in complex queries or simply to avoid performing the same operation several times in a query, by storing temporary results in a variable.
Here is a sample query expression with a let clause:

from person in persons
let name = person.FirstName+person.LastName
select new { Name = name, Phones = person.Phones.Count() }

To reproduce the same using the method syntax, you have to write the following:

persons
  .Select(person => new { person = person, name = person.FirstName+person.LastName })
  .Select(x => new { Name = x.name, Phones = x.person.Phones.Count() });

Not so easy to read, don't you think? This is what the compiler generates when it encounters the above query expression, by the way.

There are other advantages to each syntax, but the ones listed above are already enough to decide which one to use in most cases.

Cross-posted from http://linqinaction.net

5 Comments

  • Hi,

    Good post!

    I have experienced that the query syntax returns the IEnumerable or IQuerable which the method syntax can also return entities.

    I guess the question is that can I return single entities T using the query method?

  • Not really. You have to mix both syntaxes to get single results. e.g.:

    var firstAdult =
    (from person in persons
    where person.Age >= 18
    select person.Name).FirstOrDefault();

    Which isn't really looking nice, in my opinion.

  • Why not to simplify it?

    persons
    .Select(person => new { Name = person.FirstName + person.LastName, Phones = person.Phones.Count() });

  • Method syntax is good for when you want to generate LINQ queries dynamically with expression trees and such. Query syntax is sweet syntactic sugar, especially when you're doing complicated queries with many lets, joins, and nested projections.

  • Koistya, the example I gave is too simple. A let clause can be used to simplify queries, but also to avoid computing the same things multiple times.
    See this other example: http://linqinaction.net/blogs/main/archive/2007/12/05/use-the-power-of-let-in-your-linq-queries.aspx
    How would you write the query using the method syntax in this case?

Comments have been disabled for this content.