LINQ: Inner Join

一、 数据准备

复制代码
  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

 

posted @   katesharing  阅读(446)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示