C# Joins
Join pairs items from two sequences whose keys match. Each output row is built from one matched pair.
Join students to classes
Each student carries a class id. Join pairs that id with the matching class id.
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public string Name = "";
public int ClassId;
}
class SchoolClass
{
public int Id;
public string Room = "";
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student { Name = "Hana", ClassId = 2 },
new Student { Name = "Bo", ClassId = 1 },
new Student { Name = "Cy", ClassId = 2 }
};
List<SchoolClass> classes = new List<SchoolClass>
{
new SchoolClass { Id = 1, Room = "Room A" },
new SchoolClass { Id = 2, Room = "Room B" }
};
var rows = students.Join(classes, s => s.ClassId, c => c.Id,
(s, c) => s.Name + " -> " + c.Room);
foreach (string row in rows)
{
Console.WriteLine(row);
}
}
}An unmatched student drops out
Di carries class id 9 and no class holds that id. Di produces no row in the printout.
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public string Name = "";
public int ClassId;
}
class SchoolClass
{
public int Id;
public string Room = "";
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student { Name = "Hana", ClassId = 2 },
new Student { Name = "Bo", ClassId = 1 },
new Student { Name = "Di", ClassId = 9 },
new Student { Name = "Cy", ClassId = 2 }
};
List<SchoolClass> classes = new List<SchoolClass>
{
new SchoolClass { Id = 1, Room = "Room A" },
new SchoolClass { Id = 2, Room = "Room B" }
};
var rows = students.Join(classes, s => s.ClassId, c => c.Id,
(s, c) => s.Name + " -> " + c.Room);
foreach (string row in rows)
{
Console.WriteLine(row);
}
}
}Order follows the outer sequence
Classes form the outer sequence here. Rows print in class order, with matches in student order inside each class.
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public string Name = "";
public int ClassId;
}
class SchoolClass
{
public int Id;
public string Room = "";
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student { Name = "Hana", ClassId = 2 },
new Student { Name = "Bo", ClassId = 1 },
new Student { Name = "Cy", ClassId = 2 }
};
List<SchoolClass> classes = new List<SchoolClass>
{
new SchoolClass { Id = 1, Room = "Room A" },
new SchoolClass { Id = 2, Room = "Room B" }
};
var rows = classes.Join(students, c => c.Id, s => s.ClassId,
(c, s) => c.Room + ": " + s.Name);
foreach (string row in rows)
{
Console.WriteLine(row);
}
}
}Example
using System;
using System.Collections.Generic;
using System.Linq;
class Employee
{
public string Name = "";
public int DeptId;
}
class Department
{
public int Id;
public string Title = "";
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = new List<Employee>
{
new Employee { Name = "Ana", DeptId = 2 },
new Employee { Name = "Ben", DeptId = 1 },
new Employee { Name = "Fay", DeptId = 9 }
};
List<Department> departments = new List<Department>
{
new Department { Id = 1, Title = "Sales" },
new Department { Id = 2, Title = "Support" }
};
var rows = employees.Join(departments, e => e.DeptId, d => d.Id,
(e, d) => e.Name + " -> " + d.Title);
foreach (string row in rows)
{
Console.WriteLine(row);
}
}
}Key points
- Join takes an inner sequence after the outer sequence.
- One lambda reads the key from each outer item.
- One lambda reads the key from each inner item.
- A final lambda builds the output from a matched pair.
- The two keys in a pair hold equal values.
- Rows print in the order of the outer sequence.
- An item with no matching key drops out of the result.
In this example, Join pairs each employee with the department that shares the department id. Ana and Ben each find a match and print in employee order. Fay carries id 9, finds no match, and drops out.
Note
The key types on both sides must match.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A student carries a class id that no class holds. What happens in a Join?