var students = new[] { new { Id = 1, Name = "John" }, new { Id = 2, Name = "Jane" }, new { Id = 3, Name = "Bob" } }; var grades = new[] { new { StudentId = 1, Course = "Math", Score = 80 }, new { StudentId = 1, Course = "Science", Score = 90 }, new { StudentId = 2, Course = "Math", Score = 70 }, new { StudentId = 3, Course = "Science", Score = 85 } }; var query = from student in students join grade in grades on student.Id equals grade.StudentId select new { student.Name, grade.Course, grade.Score }; foreach (var result in query) { Console.WriteLine($"{result.Name} scored {result.Score} in {result.Course}"); }
var left = new[] { 1, 2, 3, 4 }; var right = new[] { 1, 3, 5 }; var query = from l in left join r in right on l equals r into matches from m in matches.DefaultIfEmpty() select new { Left = l, Right = m }; foreach (var result in query) { Console.WriteLine($"{result.Left} - {result.Right}"); }Both of these examples use the System Join library, which is a part of the System namespace in C#.