C# LINQ Joins
Connect bookings to rooms by key and state why an inner join omits unmatched bookings.
How it works
Join matches elements from two sequences by key. An inner join includes matching pairs and omits an outer record with no match. That is useful only when omission is the intended policy. An unmatched room code might instead be an error that must be reported. Duplicate keys can multiply result rows, so validate cardinality assumptions before aggregating a joined report. Query syntax and method syntax express related operations, not different storage engines.
Run it locally
Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-buildUse the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.
Example
using System;
using System.Linq;
internal static class Program
{
private static void Main(string[] args)
{
var rooms = new[] { new Room("S", "Studio"), new Room("H", "Hall") };
var bookings = new[] { new Request("S", 3), new Request("X", 2) };
var report = from booking in bookings
join room in rooms on booking.RoomCode equals room.Code
select $"{room.Name}: {booking.Places}";
foreach (string line in report) Console.WriteLine(line);
}
}
internal sealed record Room(string Code, string Name);
internal sealed record Request(string RoomCode, int Places);
Expected result
Studio: 3
Only the S booking has a matching room. The X booking is deliberately absent under this demonstrated inner-join rule; a booking importer should detect it separately before claiming a complete report. A dictionary lookup can be clearer for one-to-one validation, while a join is useful when pair relationships are the actual question.
Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.