An inner join in C# LINQ is a query that produces a result set in which each element of the first collection appears one time for every matching element in the second collection. If an element in the first collection has no matching elements, it does not appear in the result set. The Join method, which is called by the join clause in C#, implements an inner join. In this article, we will learn How to Achieve Inner Join in C# LINQ.
Before going further you may be interested in the following articles.
The diagram shows that only the elements that have a common key (in this case, the department ID) are included in the result set. The elements that do not have a matching key are excluded.
You can use inner join in C# LINQ when you want to:
Associate elements from two collections based on a common attribute or value.
Filter out the elements that do not match the other collection.
Project the results into a new form that combines the data from both collections.
How to Achieve Inner Join in C# LINQ
Follow the below steps to achieve inner join in C# LINQ:
STEP 1: Define the data sources that you want to join. For example, you can use two collections of objects, such as employees and departments.
STEP 2: Use the join keyword to join the data sources based on a common key. The key must be of the same type and name in both data sources. For example, you can join employees and departments based on the department ID.
STEP 3: Use the select keyword to project the results into a new form. You can use anonymous types or named types to create the output. For example, you can select the employee name and department name for each pair.
Here is an example to achieve Inner Join in C# LINQ:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Alice", DepartmentId = 1 },
new Employee { Id = 2, Name = "Bob", DepartmentId = 2 },
new Employee { Id = 3, Name = "Charlie", DepartmentId = null },
new Employee { Id = 4, Name = "David", DepartmentId = 3 },
new Employee { Id = 5, Name = "Eve", DepartmentId = null }
};
var departments = new List<Department>
{
new Department { Id = 1, Name = "Sales" },
new Department { Id = 2, Name = "Marketing" },
new Department { Id = 3, Name = "IT" }
};
// Perform inner join using LINQvar query = from e in employees
join d in departments
on e.DepartmentId equals d.Id
select new
{
EmployeeName = e.Name,
DepartmentName = d.Name
};
// Display the resultsforeach (var result in query)
{
Console.WriteLine($"{result.EmployeeName} works in {result.DepartmentName}");
}
Console.ReadLine();
}
}
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int? DepartmentId { get; set; }
}
class Department
{
public int Id { get; set; }
public string Name { get; set; }
}
Output:
The above code defines two classes, Employee and Department, which represent the entities being joined. It then creates two lists, employees and departments, each containing several instances of these classes.
The code then performs an inner join between the two lists using LINQ, and selects the Name properties of the Employee and Department objects that are being joined. The results of this query are stored in an anonymous type.
Finally, the code loops through the results of the query and displays them to the console. Note that the DepartmentId property of the Employee class is nullable, as some employees may not belong to a department. In the query, these employees are excluded from the results.
Bình luáºn