[C#]使用GroupJoin将两个关联的集合进行分组

本文详细介绍了C#中Join和GroupJoin操作的使用,包括它们的语法、参数说明和示例代码。Join操作用于两个序列的内部联接,而GroupJoin则进行分组联接,将第一个序列的每个元素与第二个序列中匹配的元素集合关联。示例代码展示了如何在Person和Department对象之间进行联接操作,并输出结果。

本文使用的开发环境是VS2017及dotNet4.0,写此随笔的目的是给自己及新开发人员作为参考,

对于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
)

参数说明:

outer
Type: System.Collections.Generic.IEnumerable<TOuter>
要联接的第一个序列。
inner
Type: System.Collections.Generic.IEnumerable<TInner>
要与第一个序列联接的序列。
outerKeySelector
Type: System.Func<TOuter, TKey>
用于从第一个序列的每个元素提取联接键的函数。
innerKeySelector
Type: System.Func<TInner, TKey>
用于从第二个序列的每个元素提取联接键的函数。
resultSelector
Type: System.Func<TOuter, TInner, TResult>
用于从两个匹配元素创建结果元素的函数。
返回值
Type: System.Collections.Generic.IEnumerable<TResult>
IEnumerable<T> ,其类型的元素 TResult 通过对两个序列执行内部联接获得的。

参数类型:

TOuter
第一个序列中的元素的类型。
TInner
第二个序列中的元素的类型。
TKey
键选择器函数返回的键的类型。
TResult
结果元素的类型。

例程:

using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApp33
{
    class Program
    {
        static void Main(string[] args)
        {
            GroupJoinEx();
        }
 
        static void GroupJoinEx()
        {
            Person p1 = new Person() { Name = "ABC", Age =  };
            Person p2 = new Person() { Name = "EFG", Age =  };
            Person p3 = new Person() { Name = "LMN", Age =  };
            Person p4 = new Person() { Name = "XYZ", Age =  };
 
            List<Person> pList = new List<Person> { p1, p2, p3, p4 };
 
            Department d1 = new Department() { Name = "A1", Employee = p1 };
            Department d2 = new Department() { Name = "A2", Employee = p2 };
            Department d3 = new Department() { Name = "A3", Employee = p1 };
            Department d4 = new Department() { Name = "B1", Employee = p3 };
            Department d5 = new Department() { Name = "B2", Employee = p4 };
            Department d6 = new Department() { Name = "B3", Employee = p4 };
 
            List<Department> dList = new List<Department> { d1, d2, d3, d4, d5, d6 };
 
            var result = pList.Join(dList,
                person => person,
                department => department.Employee,
                (person, department) => new
                {
                    Person = person,
                    Department = department
                });
 
            foreach(var item1 in result)
            {
                Console.Write($"Name:{item1.Person} & Department:{item1.Department} ");
                Console.WriteLine();
            }
        }
    }
 
    class Person
    {
        public string Name { set; get; }
        public int Age { set; get; }
        public override string ToString()
        {
            return $"{Name},{Age}";
        }
    }
 
    class Department
    {
        public string Name { set; get; }
        public Person Employee { set; get; }
        public override string ToString()
        {
            return $"{Name}";
        }
    }
}

输出结果:
在这里插入图片描述
对于GroupJoin的用法说明如下:

语法:

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
)

参数说明:

outer
Type: System.Collections.Generic.IEnumerable<TOuter>
要联接的第一个序列。
inner
Type: System.Collections.Generic.IEnumerable<TInner>
要与第一个序列联接的序列。
outerKeySelector
Type: System.Func<TOuter, TKey>
用于从第一个序列的每个元素提取联接键的函数。
innerKeySelector
Type: System.Func<TInner, TKey>
用于从第二个序列的每个元素提取联接键的函数。
resultSelector
Type: System.Func<TOuter, IEnumerable<TInner>, TResult>
用于从第一个序列的元素和第二个序列的匹配元素集合中创建结果元素的函数。
返回值
Type: System.Collections.Generic.IEnumerable<TResult>
IEnumerable<T> ,其中包含类型的元素 TResult 通过对两个序列执行分组的联接获得的。

参数类型:

TOuter
第一个序列中的元素的类型。
TInner
第二个序列中的元素的类型。
TKey
键选择器函数返回的键的类型。
TResult
结果元素的类型。

例程:

using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApp33
{
    class Program
    {
        static void Main(string[] args)
        {
            GroupJoinEx();
        }
 
        static void GroupJoinEx()
        {
            Person p1 = new Person() { Name = "ABC", Age =  };
            Person p2 = new Person() { Name = "EFG", Age =  };
            Person p3 = new Person() { Name = "LMN", Age =  };
            Person p4 = new Person() { Name = "XYZ", Age =  };
 
            List<Person> pList = new List<Person> { p1, p2, p3, p4 };
 
            Department d1 = new Department() { Name = "A1", Employee = p1 };
            Department d2 = new Department() { Name = "A2", Employee = p2 };
            Department d3 = new Department() { Name = "A3", Employee = p1 };
            Department d4 = new Department() { Name = "B1", Employee = p3 };
            Department d5 = new Department() { Name = "B2", Employee = p4 };
            Department d6 = new Department() { Name = "B3", Employee = p4 };
 
            List<Department> dList = new List<Department> { d1, d2, d3, d4, d5, d6 };
 
            var result = pList.GroupJoin(dList,
                person => person,
                department => department.Employee,
                (person, departments) => new
                {
                    Person = person,
                    Department = departments.Select(d => d)
                });
 
            foreach(var item1 in result)
            {
                Console.Write($"Name:{item1.Person} & ");
                foreach(var item2 in item1.Department)
                {
                    if(item1.Department.First() == item2)
                        Console.Write($"Department:{item2} ");
                    else
                        Console.Write($"{item2} ");
                }
                Console.WriteLine();
            }
        }
    }
 
    class Person
    {
        public string Name { set; get; }
        public int Age { set; get; }
        public override string ToString()
        {
            return $"{Name},{Age}";
        }
    }
 
    class Department
    {
        public string Name { set; get; }
        public Person Employee { set; get; }
        public override string ToString()
        {
            return $"{Name}";
        }
    }
}

输出结果:
在这里插入图片描述
以上代码仅在JoinGroupJoin最后一个参数有区别,可以参见红色字体部分,

并从以上结果来看,JoinGroupJoin的区别一个在于:Join仅仅是将两个结合进行关联,而GroupJoin则会进行分组。

Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等。对各个表之间的关系,就用这些实现对多个表的操作。 说明:在Join操作中,分别为Join(Join查询), SelectMany(Select一对多选择)和GroupJoin(分组Join查询)。 该扩展方法对两个序列中键匹配的元素进行inner join操作 SelectMany 说明:我们在写查询语句时,如果被翻译成SelectMany需要满足2个条件。1:查询语句中没有join和into,2:必须出现EntitySet。在我们表关系中有一对一关系,一对多关系,多对多关系等,下面分别介绍一下。 1.一对多关系(1 to Many): var q = from c in db.Customers from o in c.Orders where c.City == "London" select o; 语句描述:Customers与Orders是一对多关系。即Orders在Customers类中以EntitySet形式出现。所以第二个 from是从c.Orders而不是db.Orders里进行筛选。这个例子在From子句中使用外键导航选择伦敦客户的所有订单。 var q = from p in db.Products where p.Supplier.Country == "USA" && p.UnitsInStock == 0 select p; 语句描述:这一句使用了p.Supplier.Country条件,间接关联了Supplier表。这个例子在Where子句中使用外键导航筛选其供应商在美国且缺货的产品。生成SQL语句为: SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID],[t0].[QuantityPerUnit],[t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder],[t0].[ReorderLevel], [t0].[Discontinued] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Suppliers] AS [t1] ON [t1].[SupplierID] = [t0].[SupplierID] WHERE ([t1].[Country] = @p0) AND ([t0].[UnitsInStock] = @p1) -- @p0: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [USA] -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [0] 2.多对多关系(Many to Many): var q = from e in db.Employees from et in e.EmployeeTerritories where e.City == "Seattle" select new { e.FirstName, e.LastName, et.Territory.TerritoryDescription }; 说明:多对多关系一般会涉及三个表(如果有一个表是自关联的,那有可能只有2个表)。这一句语句涉及Employees, EmployeeTerritories, Territories三个表。它们的关系是1:M:1。Employees和Territories没有很明确的关系。 LINQ to SQL语句之Join和Order By部分代码 语句描述:这个例子在From子句中使用外键导航筛选在西雅图的雇员,同时列出其所在地区。这条生成SQL语句为: SELECT [t0].[FirstName], [t0].[LastName], [t2].[TerritoryDescription] FROM [dbo].[Employees] AS [t0] CROSS JOIN [dbo].[EmployeeTerritories] AS [t1] INNER JOIN [dbo].[Territories] AS [t2] ON [t2].[TerritoryID] = [t1].[TerritoryID] WHERE ([t0].[City] = @p0) AND ([t1].[EmployeeID] = [t0].[EmployeeID]) -- @p0: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [Seattle] 。。。。。。。。。。。。。。。。。。。。。。。。。。。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值