Home  >  Article  >  Backend Development  >  How to use LINQ and Lambda for Join operation in C#?

How to use LINQ and Lambda for Join operation in C#?

王林
王林forward
2023-08-23 09:45:021260browse

How to use LINQ and Lambda for Join operation in C#?

Inner join returns only those records or rows that match or exist in both tables. We can also apply joins on multiple tables based on conditions as shown below.

If we need to join based on multiple conditions, use anonymous types.

In the following example, we have written two methods that can be used to connect in Linq Here departments and employees are connected

Example

class Program{
   static void Main(string[] args){
      var result =
      Employee.GetAllEmployees().Join(Department.GetAllDepartments(),
      e => e.DepartmentID,
      d => d.ID, (employee, department) => new{
         EmployeeName = employee.Name,
         DepartmentName = department.Name
      });
      foreach (var employee in result){
         Console.WriteLine(employee.EmployeeName + "\t" +
         employee.DepartmentName);
      }
      var result1 = from e in Employee.GetAllEmployees()
      join d in Department.GetAllDepartments()
      on e.DepartmentID equals d.ID
      select new{
         EmployeeName = e.Name,
         DepartmentName = d.Name
      };
      foreach (var employee in result1){
         Console.WriteLine(employee.EmployeeName + "\t" +
         employee.DepartmentName);
      }
      Console.ReadLine();
   }
}
public class Employee{
   public int ID { get; set; }
   public string Name { get; set; }
   public int DepartmentID { get; set; }
   public static List<Employee> GetAllEmployees(){
      return new List<Employee>(){
         new Employee { ID = 1, Name = "A", DepartmentID = 1 },
         new Employee { ID = 2, Name = "B", DepartmentID = 2 },
         new Employee { ID = 3, Name = "B", DepartmentID = 1 },
         new Employee { ID = 4, Name = "V", DepartmentID = 1 },
         new Employee { ID = 5, Name = "F", DepartmentID = 2 },
         new Employee { ID = 6, Name = "R", DepartmentID = 2 },
         new Employee { ID = 7, Name = "TT", DepartmentID = 1 },
         new Employee { ID = 8, Name = "YY", DepartmentID = 1 },
         new Employee { ID = 9, Name = "WW", DepartmentID = 2 },
         new Employee { ID = 10, Name = "QQ"}
      };
   }
}
public class Department{
   public int ID { get; set; }
   public string Name { get; set; }
   public static List<Department> GetAllDepartments(){
      return new List<Department>(){
         new Department { ID = 1, Name = "IT"},
         new Department { ID = 2, Name = "HR"},
         new Department { ID = 3, Name = "Contract"},
      };
   }
}

Output

A IT
B HR
B IT
V IT
F HR
R HR
TT IT
YY IT
WW HR
A IT
B HR
B IT
V IT
F HR
R HR
TT IT
YY IT
WW HR

The above is the detailed content of How to use LINQ and Lambda for Join operation in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete