Codectionary / Developer documentation / C#

Match Related Sequences with Join

Build inner-join results from matching keys and choose a suitable equality comparer.

Read the syntax

Build inner-join results from matching keys and choose a suitable equality comparer.

James’s key 3 has no matching course and produces no result.

What it is and why to use it

A list of enrolments can store a course ID while a separate list stores course names. Join correlates matching keys and produces one result for each matching pair. It is an inner join: an outer entry with no match contributes no result. Duplicate keys can produce multiple matches, so a join does not imply one-to-one data.

How it works

Provide the inner sequence, an outer key selector, an inner key selector and a result selector. A comparer overload can define key equality explicitly. Enumerable.Join preserves outer sequence order and, for each outer item, matching inner order. It uses deferred execution and can buffer lookup data; it is not a streaming guarantee for infinite inputs.

Common mistakes and limitations

Do not confuse a missing match with a null-filled result; inner joins omit it. Do not assume keys are unique without validation. These examples use LINQ to Objects, not a promise about SQL translation, database null semantics or remote query performance.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

outer.Join(inner, a => a.Id, b => b.Id, (a, b) => result)

Examples

Resolve an enrolment’s course name

Match one enrolment and omit another whose course is absent.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

var learners = new[] { (Name: "John", CourseId: 1), (Name: "James", CourseId: 3) };
var courses = new[] { (Id: 1, Title: "PHP"), (Id: 2, Title: "C#") };
var lines = learners.Join(courses, learner => learner.CourseId, course => course.Id,
    (learner, course) => $"{learner.Name}: {course.Title}");
foreach (var line in lines) Console.WriteLine(line);
  1. John’s key 1 matches the PHP course.
  2. James’s key 3 has no matching course and produces no result.
  3. The result selector formats each matched pair. Use a grouped or outer-join approach when missing matches must remain visible.
  4. Expected output: John: PHP
  5. State whether unmatched entries should be retained.

Choose case-sensitive or insensitive matching

Make the key-comparison policy visible in the overload.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

string[] outer = { "php" };
string[] inner = { "PHP", "php" };
Console.WriteLine(outer.Join(inner, x => x, x => x, (a, b) => b).Count());
Console.WriteLine(outer.Join(inner, x => x, x => x, (a, b) => b,
    StringComparer.OrdinalIgnoreCase).Count());
  1. Default string equality matches only the exact lowercase key.
  2. OrdinalIgnoreCase also matches the uppercase inner key.
  3. The second result has two pairs because both inner values match; the comparer does not deduplicate joined records.
  4. Expected output: 1 2
  5. State whether unmatched entries should be retained.

Best practices

  • State whether unmatched entries should be retained.
  • Check key uniqueness and equality rules before relying on result counts.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading