Lesson 5: Joining Collections with LINQ
LINQ joins let you combine related data from multiple collections, similar to SQL joins. This is useful when working with object graphs or in-memory relational data.
Inner Join with Join
Join pairs elements from two collections when a key matches.
var students = new[]
{
new { Id = 1, Name = "Ava" },
new { Id = 2, Name = "Leo" }
};
var grades = new[]
{
new { StudentId = 1, Grade = 90 },
new { StudentId = 2, Grade = 82 }
};
var report = students.Join(
grades,
student => student.Id,
grade => grade.StudentId,
(student, grade) => new { student.Name, grade.Grade }
);
Left Join with GroupJoin
GroupJoin can simulate a left join by pairing each left item with zero or more right items.
var courses = new[]
{
new { Id = 1, Title = "C# Basics" },
new { Id = 2, Title = "LINQ Deep Dive" }
};
var enrollments = new[]
{
new { CourseId = 2, Student = "Ava" }
};
var courseRoster = courses.GroupJoin(
enrollments,
course => course.Id,
enrollment => enrollment.CourseId,
(course, studentGroup) => new
{
course.Title,
Students = studentGroup.Select(s => s.Student)
}
);
Cross Join with SelectMany
SelectMany can produce a Cartesian product when each item is paired with every item in another sequence.
var colors = new[] { "Red", "Blue" };
var sizes = new[] { "S", "M", "L" };
var variants = colors.SelectMany(color => sizes, (color, size) => new { color, size });
Real-World Match
Joining helps when you need combined results like student names with grades, order details with products, or customers with invoices.
🧠 Quick Check — Lesson 5
Which LINQ method is best for an inner join?
Lesson Summary
Join performs an inner join between two sequences using matching keys.
GroupJoin is useful for left-join style grouping of related items.
SelectMany can flatten results and generate a cross join.
LINQ joins are excellent when working with normalized in-memory data structures.