Skip to content
Tutorials Center
My learning

Topics

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.

C#
var q = from w in words
        let len = w.Length
        where len > 3
        select (w, len);

Try it

C# let: name an intermediate value

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# into: continue after group or select

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#
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

  • let is evaluated once per element and the value is carried through the rest of the query, so it replaces repeated calls such as s.Split(' ') in both where and select.
  • let can hold anything: numbers, strings, arrays, other queries. It cannot hold a ref struct such as Span<T>, because it becomes part of a compiler-generated tuple or anonymous type.
  • into resets scope. After select x into y, only y exists. If you need the earlier variables, project them into a tuple or anonymous object first.
  • group ... into g is how you filter or sort groups (where g.Count() > 1). Without into, group has to be the final clause.
  • join ... into produces the group join used for left outer joins; combine it with DefaultIfEmpty() in a following from clause. See Join() and GroupJoin().
  • Hidden allocation. Each let adds a projection step that creates a small tuple or anonymous object per element in method syntax. In hot paths this is measurable; a plain foreach loop avoids it.
  • No equivalent for many operators. into is also the tool for using methods that have no clause: (from ...).Distinct() and select x into y are interchangeable in intent.
  • Readability. If a query needs more than two or three let clauses, extract a helper method.