In C# LINQ, how does query syntax such as from … where … select translate into method syntax, and which operators have no query keyword?
answer
- a compile-time rewrite
- one clause, one method call
- range variable becomes a lambda parameter
- no keyword for Count or Distinct
- parentheses, then a dot
basics
~20 sQuery syntax is a compile-time rewrite: the C# compiler turns each clause into a method call (where to Where, select to Select, join to Join), so both forms run identical code. Distinct, Take, Count, ToList and most other operators have no keyword.
solid answer
~50 sA query expression is syntactic sugar. The compiler rewrites `from l in loans where l.Due < today select l.BookId` into `loans.Where(l => l.Due < today).Select(l => l.BookId)`; `join` becomes `Join`, `join … into` becomes `GroupJoin`, `group … by` becomes `GroupBy`, `orderby a, b` becomes `OrderBy` then `ThenBy`, and a second `from` becomes `SelectMany`. The output is ordinary method calls, so the two forms run the same operators at the same moment and perform the same; the choice is about readability. Query syntax reads better with joins, `let` and several `from` clauses, because it hides the anonymous objects the compiler threads between calls; method syntax reads better for short chains. Most operators have no keyword at all — `Distinct`, `Take`, `Skip`, `Count`, `Any`, `First`, `Sum`, `ToList` — so you wrap the query in parentheses and call them with a dot.
code
csharp · 28 linesusing System.Linq;
var today = new DateOnly(2026, 10, 10);
Author[] authors = [new(1, "Achebe"), new(2, "Le Guin"), new(3, "Morrison")];
Book[] books = [new(10, "Things Fall Apart", 1), new(11, "The Dispossessed", 2), new(12, "Beloved", 3)];
Loan[] loans = [new(10, new(2026, 10, 1)), new(11, new(2026, 10, 20)), new(12, new(2026, 9, 30)), new(12, new(2026, 10, 5))];
// Query syntax, wrapped in parentheses so Distinct (no query keyword) can follow.
var querySyntax =
(from loan in loans
where loan.Due < today
join book in books on loan.BookId equals book.Id
join author in authors on book.AuthorId equals author.Id
select author.Name).Distinct();
// Method syntax: the same calls the compiler generates for the query above.
var methodSyntax = loans
.Where(loan => loan.Due < today)
.Join(books, loan => loan.BookId, book => book.Id, (loan, book) => new { loan, book })
.Join(authors, lb => lb.book.AuthorId, author => author.Id, (lb, author) => author.Name)
.Distinct();
Console.WriteLine(string.Join(", ", querySyntax)); // Achebe, Morrison
Console.WriteLine(string.Join(", ", methodSyntax)); // Achebe, Morrison
record Author(int Id, string Name);
record Book(int Id, string Title, int AuthorId);
record Loan(int BookId, DateOnly Due);go deeper
Recall that the compiler rewrites each clause into a method call, and that Distinct, Take, Count and ToList need method syntax. Show the parentheses-then-dot way to mix the two forms.
Walk through the clause-to-method table: a second from becomes SelectMany, let becomes Select into an anonymous object, extra orderby keys become ThenBy. Stress that the output is the same calls with the same timing.
Argue the choice on readability: joins and lets read better as query expressions, short chains and comparer overloads as methods. Know the pattern-based lookup behind CS1936.
If the team wants a style rule, base it on how joins, lets and groups read in review, never on performance, since both spellings compile to the same calls.
## Two spellings of one query **LINQ** (Language-Integrated Query) gives C# two ways to write the same query over a sequence. **Method syntax** calls the standard query operators directly, as extension methods: `loans.Where(l => l.Due < today).Select(l => l.BookId)`. **Query syntax** writes a *query expression* with contextual keywords: `from l in loans where l.Due < today select l.BookId`. The second form is not a separate query engine. The compiler rewrites every query expression into method calls, turning each clause's expression into a lambda whose parameter is the **range variable** (`l` above), and then compiles those calls like any others. For the same query, both forms therefore call the same operators and run at the same moment. When the query runs is the subject of the lesson on deferred execution; it is identical for both spellings. ## Clause by clause | Query clause | Method call the compiler emits | |---|---| | `where cond` | `Where(x => cond)` | | `select expr` | `Select(x => expr)` | | a second `from y in ys` | `SelectMany(x => ys, (x, y) => …)` | | `let v = expr` | `Select(x => new { x, v = expr })` | | `orderby a, b descending` | `OrderBy(x => a).ThenByDescending(x => b)` | | `join … on k1 equals k2` | `Join(inner, x => k1, y => k2, …)` | | `join … into g` | `GroupJoin(…)` | | `group e by k` | `GroupBy(x => k, x => e)` | | `from Book b in source` | `source.Cast<Book>()` first | Two details show how literal the rewrite is: - A trailing `select b` that only returns the range variable is dropped when another clause precedes it: `from b in books where b.Year > 1960 select b` becomes just `books.Where(b => b.Year > 1960)`. - A query that is nothing but `from b in books select b` still becomes `books.Select(b => b)`, so its result is an iterator, not the original array or list. ## Operators with no keyword C# has query keywords for only part of the operator set: `from`, `where`, `select`, `group`, `into`, `orderby`, `join` and `let`, plus `in`, `on`, `equals`, `by`, `ascending` and `descending` inside those clauses. Everything else exists only as a method: - set operators: `Distinct`, `Union`, `Intersect`, `Except` - partitioning: `Take`, `Skip`, `TakeWhile`, `SkipWhile`, `Chunk` - element operators and quantifiers: `First`, `Single`, `Any`, `All`, `Contains` - aggregates: `Count`, `Sum`, `Min`, `Max`, `Average`, `Aggregate` - conversions: `ToList`, `ToArray`, `ToDictionary`, `ToLookup`, `OfType` - `Zip` and `Reverse` The keywords also have no slot for an `IEqualityComparer<T>` or `IComparer<T>`. A `group … by`, `join … equals` or `orderby` that needs a custom comparer has to be written as the method overload that takes one. ## Mixing the two forms The two spellings combine freely, because a query expression is just an expression of type `IEnumerable<T>`: 1. Write the query expression. 2. Wrap it in parentheses. 3. Call the keywordless operator with a dot: `(from l in loans where l.Due < today select l.BookId).Distinct().Count()`. Storing the query in a variable and calling the method on that variable works the same way. The opposite direction works too: the source after `in` can be any method-syntax expression, such as `from b in books.Take(50)`. ## Which form reads better - **Query syntax** pays off with joins, `let`, several `from` clauses and groups with continuations. The compiler carries every range variable forward inside hidden anonymous objects (*transparent identifiers*) that method syntax makes you name and nest by hand. - **Method syntax** pays off for one or two operators, for any operator without a keyword, and whenever a comparer is passed. - Neither form is faster. A code review that prefers one should argue readability, not speed. ## The query pattern behind the rewrite The rewrite is pattern-based: after translation, the compiler looks up methods named `Where`, `Select`, `SelectMany` and so on, as instance methods or extension methods, exactly as it would for calls you typed. `using System.Linq;` (one of the implicit global usings when a project enables `ImplicitUsings`) brings `Enumerable`'s extension methods into scope for `IEnumerable<T>`. A type with suitable `Where` and `Select` methods supports query syntax even if it implements no interface at all, and a source with no such method fails to compile with CS1936: "Could not find an implementation of the query pattern for source type 'int'. 'Select' not found." The same mechanism is why one query text binds to `Queryable` methods when its source is an `IQueryable<T>`; what that changes is covered in the lesson on IQueryable and expression trees.
- What does the C# compiler emit for a query that is only from b in books select b?It still emits `books.Select(b => b)`. An identity `select` is dropped only when another clause precedes it, so `from b in books where b.Year > 1960 select b` becomes plain `books.Where(...)`. The lone `Select` means the result is an iterator object rather than the array or list itself; casting it back to `List<Book>` fails.
- Can C# query syntax work on a type that does not implement IEnumerable<T>?Yes. Translation produces method calls, and any type with suitably shaped `Where`, `Select` or other query methods, as instance or extension methods, accepts them. A source with none fails with CS1936, "Could not find an implementation of the query pattern". The same lookup is why a query over an `IQueryable<T>` binds to `Queryable` rather than `Enumerable`.
- How do you use a custom IEqualityComparer<T> in a C# LINQ query written in query syntax?The keywords cannot take one: `group … by`, `join … equals` and `orderby` always use the default comparer. Write that step in method syntax with the overload that accepts a comparer, such as `GroupBy(keySelector, comparer)` or `Join(inner, outerKey, innerKey, resultSelector, comparer)`, either for the whole query or by passing the query expression's result into the call.
- Query syntax is slower because the keywords are interpreted at run time.
- Query syntax and method syntax call different operators under the hood.
- Every LINQ operator has a matching query keyword.
- Query syntax only works on types that implement IEnumerable<T>.
- One query cannot mix query syntax with method calls.