Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Enumerable.Join() / Zip()

Enumerable.Join() / Zip()

These are LINQ extension methods for joining two sequences. This page covers Join(), which joins by a common key, and Zip(), which pairs elements by position.

Syntax

using System.Linq;

// Performs an inner join on two sequences using a common key.
IEnumerable<TResult> joined = outer.Join(
    inner,
    outerKey => outer key,
    innerKey => inner key,
    (outer, inner) => result selector
);

// Pairs elements from two sequences by position.
IEnumerable<TResult> zipped = first.Zip(second, (a, b) => result selector);

// .NET 6 and later: Zip that returns tuple pairs without a selector
IEnumerable<(T1, T2)> zipped2 = first.Zip(second);

Method List

MethodDescription
Join(inner, outerKey, innerKey, result)Returns the transformed results of an inner join (INNER JOIN) on elements with matching keys.
GroupJoin(inner, outerKey, innerKey, result)Performs a group join equivalent to a left outer join (LEFT OUTER JOIN).
Zip(second, resultSelector)Pairs elements from two sequences by position and returns the transformed results.
Zip(second).NET 6 and later. Returns a sequence of tuples (T1, T2).

Sample Code

using System;
using System.Collections.Generic;
using System.Linq;

// Join an employee list with a department list.
var employees = new List<(int Id, string Name, int DeptId)>
{
    (1, "Tanaka", 10),
    (2, "Suzuki", 20),
    (3, "Sato", 10),
};
var departments = new List<(int Id, string DeptName)>
{
    (10, "Engineering"),
    (20, "Sales"),
};

// Join: combine rows where the department ID matches.
var joined = employees.Join(
    departments,
    e => e.DeptId,
    d => d.Id,
    (e, d) => $"{e.Name} ({d.DeptName})"
);
foreach (var s in joined) Console.WriteLine(s);

// Zip: pair two lists by position.
List<string> names  = new List<string> { "Tanaka", "Suzuki", "Sato" };
List<int>    scores = new List<int>    { 80, 95, 70 };

var zipped = names.Zip(scores, (name, score) => $"{name}: {score} pts");
foreach (var s in zipped) Console.WriteLine(s);

// .NET 6 and later: Zip returning tuples.
var pairs = names.Zip(scores);
foreach (var (name, score) in pairs)
    Console.WriteLine($"{name} = {score}");

Notes

Join() is equivalent to SQL's INNER JOIN. An element is included in the result only when a matching key exists in both the outer and inner sequences. Because the inner sequence is pre-loaded into a hash table, the join is efficient even with large datasets.

Zip() stops when the shorter sequence is exhausted. No warning is issued when the sequences have different lengths, so elements may be silently dropped. This is fine when the length difference is intentional, but if you expect both sequences to be the same length, verify their lengths beforehand.

For a left outer join (LEFT JOIN equivalent), combine GroupJoin() with SelectMany(). See Enumerable.Select() / SelectMany() for details. If you need a grouped join, also refer to Enumerable.GroupBy().

If you find any errors or copyright issues, please .