C# Reference ยท LINQ
LINQ let and into clauses
Reference for the let and into clauses in C# LINQ query syntax: name intermediate values, avoid repeated work and continue a query after select or group.
Two clauses make longer LINQ queries readable. let introduces a new range variable that holds the result of an expression, so later clauses can use the name instead of repeating the computation. into takes the result of a select, group or join and continues the query with it under a new name.
Both are query-syntax features; method syntax has no keyword for them, but the compiler turns them into a Select() that carries a tuple (for let) and into nested queries (for into). You can write the same thing by hand with anonymous types.
var q = from w in words let len = w.Length where len > 3 select (w, len);
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
let identifier = expression select expression into identifier group expression by key into identifier join item in source on a equals b into groupName
| Part | Description |
|---|---|
let identifier = expression |
Adds a read-only range variable computed for each element. Earlier range variables stay in scope. |
into identifier after select or group |
Ends the current query and begins a new one whose range variable is identifier. Earlier range variables go out of scope. |
into groupName after join |
Makes the matching elements available as a sequence (a group join). The range variable of the joined source is no longer usable. |
Returns: a lazy query like any other. Errors: reusing the name of a range variable that is already in scope is a compile-time error; a let variable cannot be assigned to.
Usage notes
letis evaluated once per element and the value is carried through the rest of the query, so it replaces repeated calls such ass.Split(' ')in bothwhereandselect.letcan hold anything: numbers, strings, arrays, other queries. It cannot hold arefstruct such asSpan<T>, because it becomes part of a compiler-generated tuple or anonymous type.intoresets scope. Afterselect x into y, onlyyexists. If you need the earlier variables, project them into a tuple or anonymous object first.group ... into gis how you filter or sort groups (where g.Count() > 1). Withoutinto,grouphas to be the final clause.join ... intoproduces the group join used for left outer joins; combine it withDefaultIfEmpty()in a followingfromclause. SeeJoin()andGroupJoin().- Hidden allocation. Each
letadds a projection step that creates a small tuple or anonymous object per element in method syntax. In hot paths this is measurable; a plainforeachloop avoids it. - No equivalent for many operators.
intois also the tool for using methods that have no clause:(from ...).Distinct()andselect x into yare interchangeable in intent. - Readability. If a query needs more than two or three
letclauses, extract a helper method.