Joining

Method

Description

SQL equivalents

Join

Applies a lookup strategy to match elements from two collections, emitting a flat result set

INNER JOIN

Group Join

As above, but emits a hierarchical result set

INNER JOIN, LEFT OUTER JOIN

Join and GroupJoin

Join arguments

Argument

Type

Outer sequence

IEnumerable<TOuter>

Inner sequence

IEnumerable<TInner>

Outer key selector

TOuter => TKey

Inner key selector

TInner => TKey

Result selector

(TOuter,TInner) => TResult

GroupJoin arguments

Argument

Type

Outer sequence

IEnumerable<TOuter>

Inner sequence

IEnumerable<TInner>

Outer key selector

TOuter => TKey

Inner key selector

TInner => TKey

Result selector

(TOuter,IEnumerable<TInner>) => Tresult

Return type = IEnumerable<TResult>

Comprehension syntax

	from outer-var in outer-enumerable
	join inner-var in inner-enumerable
	  on outer-key-expr equals inner-key-expr
	 [ into identifier ]

Overview

Join and GroupJoin mesh two input sequences into a single output sequence. Join emits flat output; GroupJoin emits hierarchical output.

Join and GroupJoin provide an alternative strategy to Select and SelectMany. The advantage of Join and GroupJoin is that they execute efficiently over local in-memory collections because they first load the inner sequence into a keyed lookup, avoiding the need to repeatedly enumerate over every inner element. Their disadvantage is that they offer the equivalent of inner and left outer joins only; cross joins and non-equi joins must still be done with Select /SelectMany. With LINQ to SQL ...

Get LINQ Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.