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.
var adults = from p in people where p.Age >= 18 orderby p.Name select p.Name;
Try it
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.
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
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 defineSelect,Whereand 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 useinto. - Range variable scope. A variable declared in
from,letorjoinis visible in later clauses, but not afterselect ... into. equalsis required in joins.join ... on a == bdoes not compile; the sides ofequalsmust reference different range variables.- Contextual keywords.
from,where,select,group,into,orderby,join,let,on,equals,by,ascendinganddescendingare keywords only inside query expressions, so they remain valid identifiers elsewhere. - Anonymous types are common in
selectclauses: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.