C# Practice Exercises

Join Customers and Orders

Combine related sequences with LINQ Join.

Introduction

Join matches keys across two typed collections much like a relational database join.

The key selectors choose CustomerId from each sequence and the result selector shapes each match.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Linq;
record Customer(int Id, string Name);
record Order(int CustomerId, decimal Total);
class Program
{
    static void Main()
    {
        var customers = new[] { new Customer(1, "Ada"), new Customer(2, "Lin") };
        var orders = new[] { new Order(2, 40m), new Order(1, 25m) };
        var rows = customers.Join(orders, c => c.Id, o => o.CustomerId, (c, o) => $"{c.Name}: {o.Total}");
        Console.WriteLine(string.Join(" | ", rows));
    }
}
Result of running the code
Ada: 25 | Lin: 40

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges