Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ query syntax

Reference for C# LINQ query syntax: from, where, select, orderby, group by and join clauses, how they compile to method calls, and when to use them.

Query syntax is the SQL-like way to write LINQ. A query starts with a from clause that declares a range variable and ends with either a select or a group ... by clause. Between them you can filter with where, sort with orderby, join sources with join, add more sources with extra from clauses, and name intermediate values with let.

It is only syntax. The compiler rewrites every query into ordinary calls to Where(), Select(), OrderBy(), Join() and the other operators, so the two styles are interchangeable and can be mixed. Many developers prefer query syntax for queries with several joins or let clauses, and method syntax for short chains.

C#
var adults =
    from p in people
    where p.Age >= 18
    orderby p.Name
    select p.Name;

Try it

C# Filter, sort and project

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Group and join

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
from [Type] range in source
    [from [Type] range2 in source2 | join ... | let ... | where ... | orderby ...]*
select expression | group expression by key [into name continuation]
Clause Compiles to
from x in s select f(x) s.Select(x => f(x))
where cond Where(x => cond)
orderby a, b descending OrderBy(...).ThenByDescending(...)
second from y in x.Items SelectMany(...)
join y in t on k1 equals k2 Join(...)
join y in t on k1 equals k2 into g GroupJoin(...)
group e by k GroupBy(k, e)
let v = expr a Select() that carries a tuple with the new value
from Type x in s s.Cast<Type>() first
... into name starts a new query using the result so far

Returns: an IEnumerable<T> or IQueryable<T> (a lazy query). A select of the range variable alone (select x) is dropped after a where or orderby.

Errors: none specific; a null source throws when the query is evaluated or built, as with the method operators.

Usage notes

  • Pure syntax. The compiler needs methods with the right names and shapes, not IEnumerable<T> specifically, so query syntax also works on custom types that define Select, Where and so on.
  • Deferred execution applies exactly as with method syntax; see deferred execution.
  • Operators without a keyword. Count(), Take(), Distinct(), First() and many others have no clause. Wrap the query in parentheses and call the method: (from x in xs select x).Distinct(), or use into.
  • Range variable scope. A variable declared in from, let or join is visible in later clauses, but not after select ... into.
  • equals is required in joins. join ... on a == b does not compile; the sides of equals must reference different range variables.
  • Contextual keywords. from, where, select, group, into, orderby, join, let, on, equals, by, ascending and descending are keywords only inside query expressions, so they remain valid identifiers elsewhere.
  • Anonymous types are common in select clauses: select new { p.Name, Total = p.Items.Sum() }.
  • Debugging. Hover over a query or look at the IL: each clause becomes a lambda, so closures and captured variables behave the same.