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

LINQ:Group Join

时间:2022-08-25 18:23:37浏览次数:61  
标签:Group Name LINQ public Join Employee new ID

一、数据准备

public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int AddressId { get; set; }
        public int DepartmentId { get; set; }
        public static List<Employee> GetAllEmployees()
        {
            return new List<Employee>()
            {
               new Employee { ID = 1, Name = "张三", AddressId = 1,DepartmentId=10},
                new Employee { ID = 2, Name = "李四", AddressId =2,DepartmentId=20},
                new Employee { ID = 3, Name = "王五", AddressId = 0,DepartmentId=0},
                new Employee { ID = 4, Name = "钱六", AddressId = 0,DepartmentId=0},
                new Employee { ID = 5, Name = "郑七", AddressId = 5,DepartmentId=0},
                new Employee { ID = 6, Name = "苏八", AddressId = 6,DepartmentId=10}
            };
        }
    }
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 = 10, Name = "IT"},
                    new Department { ID = 20, Name = "HR"},
                    new Department { ID = 30, Name = "Sales"  },
                };
        }
    }

二、Group Join

1.法一Query Syntax

 var GroupJoinQS = from dept in Department.GetAllDepartments()
                              join emp in Employee.GetAllEmployees()
                              on dept.ID equals emp.DepartmentId
                              into EmployeeGroups
                              select new { dept, EmployeeGroups };

            //Outer Foreach is for all department
            foreach (var item in GroupJoinQS)
            {
                Console.WriteLine("Department :" + item.dept.Name);
                //Inner Foreach loop for each employee of a department
                foreach (var employee in item.EmployeeGroups)
                {
                    Console.WriteLine("  EmployeeID : " + employee.ID + " , Name : " + employee.Name);
                }
            }

 

2.法二Method Syntax

  var GroupJoinMethod = Department.GetAllDepartments()
             .GroupJoin(
             Employee.GetAllEmployees(),
             dept => dept.ID,
             emp => emp.DepartmentId,
             (dept, emp) => new { dept, emp }
             );
            foreach (var item in GroupJoinMethod)
            {
                Console.WriteLine("Department :" + item.dept.Name);
                //Inner Foreach loop for each employee of a department
                foreach (var employee in item.emp)
                {
                    Console.WriteLine("  EmployeeID : " + employee.ID + " , Name : " + employee.Name);
                }
            }

 

 官方解释

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

 

 三、测试结果

 

 注意上面虽然Sales这个部门对应的Employee为空,但还是显示出来,而且发现,显示的结果是分组好的。(其实是LEFT JOIN,只是LEFT JOIN没有分组,可以这么理解吗?)

四、Group Join, Inner Join和Left Join之间的区别

1. 与上文的Inner Join对比?

 

 注意Inner Join是没分组的,所以foreach只有一层,而且inner join是不会显示出Sales这个部门,结果如下:

 

 2. 与下文Left Join对比?

 

 注意Left Join也是没分组的,所以foreach只有一层,会显示Sales这个部门,但一定一定要注意判断为空

 五、参考网址

https://dotnettutorials.net/lesson/linq-group-join/

标签:Group,Name,LINQ,public,Join,Employee,new,ID
From: https://www.cnblogs.com/keeplearningandsharing/p/16625086.html

相关文章

  • LINQ: Inner Join
    一、数据准备publicclassEmployee{publicintID{get;set;}publicstringName{get;set;}publicintAddressId{get;s......
  • webpack中path.join()和path.resolve()区别
    constpath=require('path')path模块提供了用于处理文件和目录的路径的实用工具,使用时引入即可。 1.__dirname和__filename的区别__dirname,是一个成员,用来动态获取......
  • ForkJoinPool源码分析之三(ForkJoinTask源码)
    一、类结构及其成员变量1.1类结构和注释类前面的注释部分如下:ForkJoinTask是在ForkJoinPool中运行task的基础抽象类,ForkJoinTask是类似于线程的实体,其权重比普通线程要......
  • ForkJoinPool源码分析之四(ForkJoinWorkerThread源码)
    一、类结构及其成员变量1.1类结构和注释类结构代码如下:publicclassForkJoinWorkerThreadextendsThread{}ForkJoinWorkerThread继承了Thread类,ForkJoinWo......
  • mysql按日期分组(group by)查询统计的时候,没有数据补0的解决办法。
    echarts折线图所需要的数据时间坐标是连续的,但数据库的数据在没有某天的数据的时候查出来的是不连续的解决之前的查询SELECTSUM(amount_data)num,group_codecode,p......
  • LINQ:Group Join
    1.代码//groupjoinvargroupJoinQS=fromaddrinAddress.GetAddress()joinempinEmployee.GetAllEmployees()onad......
  • Linq-面试题
    1.用逗号分隔表示成绩的字符串,计算成绩的平均值//10,60,30,20,70,90stringstr="10,60,30,20,70,90";string[]strs=str.Split(',');IEnumerable<int>nums=strs......
  • LINQ 左连接(Left Join)
     1.数据准备usingSystem.Collections.Generic;namespaceLINQTutorial{publicclassEmployee{publicintID{get;set;}publics......
  • springboot+mybatis-plus-join+mysql实现连表查询
    1.简介  Mybatis是目前比较主流的持久层框架,使用非常广泛。Mybatis-Plus是基于Mybatis增强工具包,越来越受到开发人员的喜爱。  在使用Mybatis-Plus开发时,简单的crud......
  • Lambda方式左连接有Linq方式左连接
    http://t.zoukankan.com/superfeeling-p-7530549.htmlhttps://blog.csdn.net/qq_22325259/article/details/121545038 网上查到的直接使用Join+DefaultIfEmpty的方式是......