Glossary / Query versus method syntax
Query versus method syntax
C# LINQ offers query syntax (from, where, select, join, group) and method syntax (extension calls such as Where and Select). Query expressions are translated by the compiler into method calls; they do not define a separate execution engine, though syntax can influence which standard query operators are invoked.
Syntax and translation
var querySyntax = from person in people
where person.Age >= 18
select person.Name;
var methodSyntax = people.Where(person => person.Age >= 18)
.Select(person => person.Name);
Query syntax is often readable for joins, grouping, and let clauses. Method syntax exposes the complete operator set, supports fluent composition, and is required for operators without query keywords such as Count, Any, and many overloads. Mixed syntax is allowed but can reduce clarity. The source type and extension method resolution determine whether Enumerable or Queryable operators apply.
The compiler translates query clauses using methods such as Where, Select, SelectMany, Join, and GroupJoin; exact lowering should be understood when debugging overloads or provider translation. Lambdas in query syntax may become expression trees for IQueryable sources.
Engineering guidance
Choose the syntax that makes transformations clear and consistent. Use query syntax for complex relational flows and method chains for simple pipelines or specialized operators. Check inferred types at each stage and inspect provider output when translation matters.
Interview questions
- Are these separate LINQ engines? No; query syntax lowers to method calls.
- Which supports all operators? Method syntax exposes all extension methods; query syntax has a subset mapped to clauses.
- When query syntax is clearer? Multi-join/group flows with range variables.
- What determines Enumerable vs Queryable? Static source type and selected extension overload.
- Can they be mixed? Yes, though consistency improves readability.