Esercizi C#

Join Customers and Orders

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

How it works

Combine related sequences with LINQ 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));
    }
}
Expected output
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.