LINQ:Group Join

1. 数据准备

Employee类如下:(构造数据时,特意把奇数ID的Employee设置成没有Department)

复制代码
  public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int AddressId { get; set; }
        public int DepartmentId { get; set; }
        public int Salary { get; set; }
        public static List<Employee> GetAllEmployees()
        {
            return new List<Employee>()
            {
              new Employee { ID = 1, Name = "张一", AddressId = 1,Salary = 10000, DepartmentId=0},
                new Employee { ID = 2, Name = "张二", AddressId =2,Salary = 20000, DepartmentId=20},
                new Employee { ID = 3, Name = "张三", AddressId = 0,Salary = 15000, DepartmentId=0},
                new Employee { ID = 4, Name = "张四", AddressId = 0,Salary = 10000, DepartmentId=30},
                new Employee { ID = 5, Name = "张五", AddressId = 5,Salary = 30000, DepartmentId=40},
                new Employee { ID = 6, Name = "张六", AddressId = 6,Salary = 50000, DepartmentId=10},
                new Employee { ID = 7, Name = "张七", AddressId = 1,Salary = 10000, DepartmentId=50},
                new Employee { ID = 8, Name = "张八", AddressId =2,Salary = 20000, DepartmentId=20},
                new Employee { ID = 9, Name = "张九", AddressId = 0,Salary = 15000, DepartmentId=0},
                new Employee { ID = 10, Name = "张十", AddressId = 0,Salary = 10000, DepartmentId=30},
            };
        }
    }
复制代码

 Department类如下:(构造数据时, Testing这个底下是没有employee的)

复制代码
  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"  },
                    new Department { ID = 60, Name = "Testing"  },
                };
        }
    }
复制代码

2. Group Join代码如下

复制代码
 static void GroupJoinExp()
        {
            //方式一:Query Syntax
            Console.WriteLine("方式一:Query Syntax");
            var GroupJoinQS = from dept in Department.GetAllDepartments()//Outer Data Source i.e. Departments
                              join emp in Employee.GetAllEmployees()//Joining with Inner Data Source i.e. Employees
                              on dept.ID equals emp.DepartmentId //Joining Condition
                              into EmployeeGroups //Projecting the Joining Result into EmployeeGroups
                              select new { dept, EmployeeGroups };//Final Result include each department and the corresponding employees

            //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);
                }
            }

            //方式二:Method Syntax
            Console.WriteLine("方式二:Method Syntax");
            var GroupJoinMethod = Department.GetAllDepartments()//Outer Data Source
             .GroupJoin( //Performing Group Join with Inner Data Source
             Employee.GetAllEmployees(),//Inner Data Source
             dept => dept.ID,//Outer Key Selector
             emp => emp.DepartmentId,//Inner Key Selector
             (dept, emp) => new { dept, emp }//Projecting the Result to an Anonymous Type
             );
            //Printing the Result set
            //Outer Foreach is for Each department
            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);
                }
            }
        }
复制代码

3. 测试结果如下

参考:

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

代码:CSharpBasic...LINQTutorial

posted @   katesharing  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示