首页 > 其他分享 >LINQ: Inner Join

LINQ: Inner Join

时间:2022-08-25 17:46:30浏览次数:55  
标签:Join AddressLine LINQ public Inner employee new ID Name

一、 数据准备

  public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int AddressId { get; set; }
        public static List<Employee> GetAllEmployees()
        {
            return new List<Employee>()
            {
              new Employee { ID = 1, Name = "张三", AddressId = 1},
                new Employee { ID = 2, Name = "李四", AddressId =2},
                new Employee { ID = 3, Name = "王五", AddressId = 0},
                new Employee { ID = 4, Name = "钱六", AddressId = 0},
                new Employee { ID = 5, Name = "郑七", AddressId = 5},
                new Employee { ID = 6, Name = "苏八", AddressId = 6}
            };
        }
    }
    public class Address
    {
        public int ID { get; set; }
        public string AddressLine { get; set; }
        public static List<Address> GetAllAddress()
        {
            return new List<Address>()
            {
                new Address { ID = 1, AddressLine = "地址一"},
                new Address { ID = 2, AddressLine = "地址二"},
                new Address { ID = 5, AddressLine = "地址五"},
                new Address { ID = 6, AddressLine = "地址六"},
            };
        }
    }

二、Inner Join用法

1.法一用Query syntax写法如下:

 //方式一:Query Syntax
            var InnerJoinUsingQS = from emp in Employee.GetAllEmployees()
                                   join addr in Address.GetAllAddress() on emp.AddressId equals addr.ID
                                   select new
                                   {
                                       EmployeeName = emp.Name,
                                       AddressLine = addr.AddressLine
                                   };
            foreach (var employee in InnerJoinUsingQS)
            {
                Console.WriteLine($"Name :{employee.EmployeeName}, Address : {employee.AddressLine}");
            }

2.法二用Method Syntax写法如下:

       //方式二:Method Syntax:
            var InnerJoinUsingMethod = Employee.GetAllEmployees().Join(
                Address.GetAllAddress(),
                employee => employee.AddressId,
                address => address.ID,
                (employee, address) => new
                {
                    EmployeeName = employee.Name,
                    AddressLine = address.AddressLine
                });
            foreach (var employee in InnerJoinUsingMethod)
            {
                Console.WriteLine($"Name :{employee.EmployeeName}, Address : {employee.AddressLine}");
            }

 1) 此例解释

 

 

   由上可见用Join需要理解下面几个概念

  1. Outer data source
  2. Inner data source
  3. Outer Key selector (common key in the outer data source)
  4. Inner Key selector (Common key in the inner data source)
  5. Result selector (project the data into a result set)

2)Join的官方解释

public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector);

对于这个方法的官方解释:

 

 

 三、测试结果

四、参考网址

https://dotnettutorials.net/lesson/inner-join-in-linq/

自己代码例子:CSharpBasic\LINQTutorial

 

标签:Join,AddressLine,LINQ,public,Inner,employee,new,ID,Name
From: https://www.cnblogs.com/keeplearningandsharing/p/16624822.html

相关文章