Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The into contextual keyword in C# is used within LINQ (Language Integrated Query) expressions to create a temporary identifier that stores the results of a group, join, or select clause. This mechanism, known as a query continuation, allows subsequent query operations to be evaluated against the newly formed sequence.

Core Mechanics and Scope

The primary mechanical function of into is scope mutation. When into is applied after a select or group clause, it terminates the lexical scope of the preceding range variable. The new identifier introduced by into becomes the sole active range variable for the remainder of the query expression. When used with a join clause, it alters the join type from an inner join to a group join, encapsulating the correlated elements into an IEnumerable<T> collection while keeping the outer range variable in scope.

Syntactical Contexts

The into keyword is syntactically valid in three specific query expression structures.

1. Group Continuation (group ... by ... into)

When applied to a group clause, into captures the resulting IGrouping<TKey, TElement> objects. The original range variable is discarded.
var query = from element in source
            group element by element.Category into groupedSequence
            // 'element' is now out of scope.
            // 'groupedSequence' is the active range variable of type IGrouping<TKey, TElement>.
            where groupedSequence.Count() > 5
            select groupedSequence.Key;

2. Group Join (join ... in ... on ... equals ... into)

When applied to a join clause, into instructs the compiler to perform a GroupJoin rather than a standard Join. It aggregates all matching elements from the inner sequence into a collection for each element in the outer sequence.
var query = from outer in outerSource
            join inner in innerSource on outer.Id equals inner.OuterId into joinedSequence
            // 'outer' remains in scope.
            // 'inner' is out of scope.
            // 'joinedSequence' is an IEnumerable<TInner> containing all matches for the current 'outer'.
            select new { outer.Id, Matches = joinedSequence };

3. Select Continuation (select ... into)

When applied to a select clause, into captures the projected anonymous types or objects, allowing further filtering, grouping, or projection on the intermediate result.
var query = from element in source
            select new { element.Id, TransformedValue = element.Value * 2 } into projectedSequence
            // 'element' is now out of scope.
            // 'projectedSequence' is the active range variable representing the anonymous type.
            where projectedSequence.TransformedValue > 100
            select projectedSequence;

Compiler Translation

At compile time, the C# compiler translates query expressions containing into into chained standard query operator method calls.
  • A select ... into or group ... into continuation is translated into sequential method chains (e.g., .Select(...).Where(...) or .GroupBy(...).Where(...)).
  • A join ... into expression is explicitly mapped to the Enumerable.GroupJoin extension method, bypassing the Enumerable.Join method entirely.
Master C# with Deep Grasping Methodology!Learn More