The Join
operator is a LINQ query operator that is used to combine the elements of two sequences based on a specified key. The key is specified by a function that takes an element from each sequence as input and returns a value that is used to match the elements.
Here are some examples of how you might use the Join
operator in C#:
Example 1: Joining a list of customers with a list of orders based on the customer’s ID:
1 2 3 4 5 6 7 8 |
List<Customer> customers = GetCustomers(); List<Order> orders = GetOrders(); var customerOrders = customers.Join(orders, c => c.Id, o => o.CustomerId, (c, o) => new { Customer = c, Order = o }); |
Example 2: Joining a list of employees with a list of departments based on the employee’s department ID:
1 2 3 4 5 6 7 8 |
List<Employee> employees = GetEmployees(); List<Department> departments = GetDepartments(); var employeeDepartments = employees.Join(departments, e => e.DepartmentId, d => d.Id, (e, d) => new { Employee = e, Department = d }); |
Example 3: Joining two lists of products based on the product’s name and creating a new list of the matched products:
1 2 3 4 5 6 7 8 |
List<Product> products1 = GetProducts1(); List<Product> products2 = GetProducts2(); var matchedProducts = products1.Join(products2, p1 => p1.Name, p2 => p2.Name, (p1, p2) => new { Product1 = p1, Product2 = p2 }); |
Example 4: Joining two lists of objects and applying a filter on the results based on a condition
1 2 3 4 5 6 7 8 |
List<Student> students = GetStudents(); List<Course> courses = GetCourses(); var studentCourses = students.Join(courses, s => s.Id, c => c.StudentId, (s, c) => new { Student = s, Course = c }).Where(sc => sc.Course.IsCompleted); |
The Join
operator is used to combine the elements of two sequences based on a specified key. The key is specified by a function that takes an element from each sequence as input and returns a value that is used to match the elements. In the first example the Join
operator is used to join a list of customers with a list of orders based on the customer’s ID, resulting in a new sequence of anonymous objects that contain the customer and the corresponding order. In the second example, the Join
operator is used to join a list of employees with a list of departments based on the employee’s department ID, resulting in a new sequence of anonymous objects that contain the employee and the corresponding department. In the third example, the Join
operator is used to join two lists of products based on the product’s name and creating a new list of the matched products. In the fourth example, the Join
operator is used to join two lists of objects, then Where
operator is used to filter the results based on a condition.
It’s worth noting that the Join
operator returns an IEnumerable<TResult>
object, which is a sequence of TResult
objects, where TResult is the type of the elements in the result sequence. It’s also worth noting that the join operation can be done using multiple different types of joins (Inner Join, Left Join, Right Join, and Full Join) but in LINQ we use Join
operator for Inner Join.
[…] Join: Combines the elements of two sequences based on a specified key […]