📜  LINQ |加入(内部加入)

📅  最后修改于: 2021-05-29 14:21:07             🧑  作者: Mango

在LINQ中,Join运算符用于将两个数据源集成到一个共享某些公共属性的数据源中。例如,在银行中,经理有两个列表,第一个列表包含个人详细信息,另一个列表包含房屋贷款详细信息。现在,经理想要创建一个列表,其中包含那些借了房屋贷款并属于同一地方的人的名字,因此他使用join子句来创建这种类型的列表。
join子句始终使用两个数据源,该数据源中存在的元素必须包含一些属性,以便它可以与其他数据源进行比较。 join子句的结果取决于所使用的join子句的类型。联接的最常见类型是:

  1. 内部联接
  2. 交叉连接
  3. 左外连接
  4. 团体加入

内部联接

在LINQ中,内部联接用于提供结果,该结果仅包含来自第一数据源的元素,这些元素仅在第二数据源中出现一次。并且,如果第一个数据源的元素没有匹配的元素,则它将不会出现在结果数据集中。这里的Join和Inner join相同。

重要事项:

  • 所有通过join子句执行的联接都是等联接。
  • 它支持C#和VB.Net语言的查询语法。

    句法:

    join … in … 
    on … equals …
    
  • 它支持C#和VB.Net语言的方法语法。在这里,join方法以两种不同的方式被重载:
    1. 加入(IEnumerable ,IEnumerable ,Func ,Func ,Func ):此方法允许您根据给定的匹配关键字将两个序列的元素相关联。在这里,默认的相等比较器用于比较键。
    2. Join (IEnumerable ,IEnumerable ,Func ,Func ,Func ,IEqualityComparer ):使用此方法,您可以基于匹配键来关联两个序列的元素。指定的IEqualityComparer 用于比较键。
  • Queryable和Enumerable类中都存在Join方法。
  • Join的工作类似于SQL中的内部联接。
  • join子句根据使用equals关键字指定的键比较数据源,此处==无效。

范例1:

// C# program to illustrate the concept
// of inner join in Query Syntax
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee1 {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
    public string emp_lang
    {
        get;
        set;
    }
}
  
// Employee department details
public class Employee2 {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_dept
    {
        get;
        set;
    }
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List emp1 = new List() {
  
            new Employee1() {emp_id = 300, emp_name = "Anu",
                                           emp_lang = "C#"},
  
            new Employee1() {emp_id = 301, emp_name = "Mohit",
                                              emp_lang = "C"},
  
            new Employee1() {emp_id = 302, emp_name = "Sona",
                                          emp_lang = "Java"},
                                            
            new Employee1() {emp_id = 303, emp_name = "Lana",
                                          emp_lang = "Java"},
                                            
            new Employee1() {emp_id = 304, emp_name = "Lion",
                                            emp_lang = "C#"},
                                              
            new Employee1() {emp_id = 305, emp_name = "Ramona",
                                             emp_lang = "Java"},
  
        };
  
        List emp2 = new List() {
  
            new Employee2() {emp_id = 300, emp_dept = "Designing",
                                              emp_salary = 23000},
  
            new Employee2() {emp_id = 301, emp_dept = "Developing",
                                               emp_salary = 40000},
  
            new Employee2() {emp_id = 302, emp_dept = "HR",
                                       emp_salary = 50000},
  
            new Employee2() {emp_id = 303, emp_dept = "Designing",
                                              emp_salary = 60000},
  
        };
  
        // Query to find the name and
        // the salary of the employees
        // Using Inner Join
        var res = from e1 in emp1
                    join e2 in emp2
                        on e1.emp_id equals e2.emp_id
                            select new 
                            {
                                Emp_Name = e1.emp_name,
                                Emp_Salary = e2.emp_salary
                            };
  
        // Display result
        Console.WriteLine("Employee and their Salary: ");
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0} Salary: {1}",
                                 val.Emp_Name, val.Emp_Salary);
        }
    }
}
输出:
Employee and their Salary: 
Employee Name: Anu Salary: 23000
Employee Name: Mohit Salary: 40000
Employee Name: Sona Salary: 50000
Employee Name: Lana Salary: 60000

范例2:

// C# program to illustrate the concept
// of inner join in Method Syntax
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee1 {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
    public string emp_lang
    {
        get;
        set;
    }
}
  
// Employee department details
public class Employee2 {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_dept
    {
        get;
        set;
    }
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List emp1 = new List() {
  
            new Employee1() {emp_id = 300, emp_name = "Anu",
                                           emp_lang = "C#"},
  
            new Employee1() {emp_id = 301, emp_name = "Mohit",
                                              emp_lang = "C"},
  
            new Employee1() {emp_id = 302, emp_name = "Sona",
                                          emp_lang = "Java"},
                              
            new Employee1() {emp_id = 303, emp_name = "Lana",
                                          emp_lang = "Java"},
                                            
            new Employee1() {emp_id = 304, emp_name = "Lion",
                                            emp_lang = "C#"},
                                              
            new Employee1() {emp_id = 305, emp_name = "Ramona",
                                            emp_lang = "Java"},
  
        };
  
        List emp2 = new List() {
  
            new Employee2() {emp_id = 300, emp_dept = "Designing",
                                              emp_salary = 23000},
  
            new Employee2() {emp_id = 301, emp_dept = "Developing",
                                               emp_salary = 40000},
  
            new Employee2() {emp_id = 302, emp_dept = "HR",
                                       emp_salary = 50000},
  
            new Employee2() {emp_id = 303, emp_dept = "Designing",
                                              emp_salary = 60000},
  
        };
  
        // Query to find the name and
        // the department of the employees
        // Using Join Method
        var res = emp1.Join(emp2,
                            e1 => e1.emp_id,
                            e2 => e2.emp_id,
                            (e1, e2) => new {
                                EmployeeName = e1.emp_name,
                                EmployeeDepartment = e2.emp_dept });
  
        // Display result
        Console.WriteLine("Employee Name and their Department:");
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0} Department: {1}",
                         val.EmployeeName, val.EmployeeDepartment);
        }
    }
}
输出:
Employee Name and their Department:
Employee Name: Anu Department: Designing
Employee Name: Mohit Department: Developing
Employee Name: Sona Department: HR
Employee Name: Lana Department: Designing