While writing a LINQ query, what should be guided by when choosing the style of writing a query? Use query syntax ( Query Syntax ) or Method ( Method Syntax )? What is better to use?

In my opinion, it is more convenient to write and read methods, which cannot be said about the syntax of the query. In which cases it is necessary to use the syntax of the query, and in what syntax of the method?

I would very much like to learn more and with examples, when and the better this or that approach of writing a query. From this article, the Query Syntax and Method Syntax in LINQ (C #) for some reason did not understand everything, and there are places where my opinion is different.

  • query syntax - is translated to method syntax, so there is no difference. How more like and write - Grundy
  • @Grundy, that is, there is no gain in performance, for example, there is no speed of execution or building a query based on the code? - Denis Bubnov
  • Absolutely no difference - Grundy

1 answer 1

Long and complex queries in the syntax of methods can become unreadable - especially with the frequent use of anonymous classes. The same query syntax hides these anonymous classes from you:

 var q1 = trees.SelectMany(t => t.Leaves.Select(l => new { tree = t, leaf = l })) .OrderBy(x => x.leaf.color) .Select(x => x.tree.name); 

Try rewriting the query above without an anonymous class. But in longer queries, either nested anonymous classes may appear - or permanent copies from one anonymous class to another.

When using the query syntax, many anonymous classes successfully hide:

 var q1 = from tree in trees from leaf in tree.Leaves order by leaf.Color select tree.Name; 

Also in long queries, when using methods, you can quickly get bored with writing the same prefix for each closure - in the query syntax the same range variable is defined only once.

For this reason, in complex queries, query syntax is usually preferable.


On the other hand, some things are simply impossible to do in the query syntax.

For example, method overloads that take IEqualityComparer can only be called as methods.

Also, the syntax of the methods allows you to "accumulate" a complex query in a variable depending on external conditions - which is useful when processing filtering blocks:

 if (filters.Foo.HasValue) q = q.Where(x => x.Foo == filters.Foo); if (filters.Bar != null) q = q.Where(x => x.Bar.Name == filters.Bar); 
  • four
    and sometimes it is not enough to use the overloads in query syntax, which take in Index callback - Grundy