这篇文章主要为大家展示了“C#中如何使用Join与GroupJoin将两个集合进行关联与分组”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#中如何使用Join与GroupJoin将两个集合进行关联与分组”这篇文章吧。
临西ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!对于Join的用法说明如下:
语法:
public static IEnumerableJoin ( this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector )
参数说明:
outer Type: System.Collections.Generic.IEnumerable要联接的第一个序列。 inner Type: System.Collections.Generic.IEnumerable 要与第一个序列联接的序列。 outerKeySelector Type: System.Func 用于从第一个序列的每个元素提取联接键的函数。 innerKeySelector Type: System.Func 用于从第二个序列的每个元素提取联接键的函数。 resultSelector Type: System.Func 用于从两个匹配元素创建结果元素的函数。 返回值 Type: System.Collections.Generic.IEnumerable IEnumerable ,其类型的元素 TResult 通过对两个序列执行内部联接获得的。
参数类型:
TOuter 第一个序列中的元素的类型。 TInner 第二个序列中的元素的类型。 TKey 键选择器函数返回的键的类型。 TResult 结果元素的类型。
参考链接如下:
https://msdn.microsoft.com/zh-cn/library/bb534675.aspx
https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.join?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(System.Linq.Enumerable.Join%60%604);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.7.1
例程:
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 = 18 };
Person p2 = new Person() { Name = "EFG", Age = 19 };
Person p3 = new Person() { Name = "LMN", Age = 20 };
Person p4 = new Person() { Name = "XYZ", Age = 21 };
List pList = new List { 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 dList = new List { 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 IEnumerableGroupJoin ( this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func , TResult> resultSelector )
参数说明:
outer Type: System.Collections.Generic.IEnumerable
要联接的第一个序列。
inner Type: System.Collections.Generic.IEnumerable
要与第一个序列联接的序列。
outerKeySelector Type: System.Func
用于从第一个序列的每个元素提取联接键的函数。
innerKeySelector Type: System.Func
用于从第二个序列的每个元素提取联接键的函数。
resultSelector Type: System.Func, TResult>
用于从第一个序列的元素和第二个序列的匹配元素集合中创建结果元素的函数。
返回值
Type: System.Collections.Generic.IEnumerableIEnumerable ,其中包含类型的元素 TResult 通过对两个序列执行分组的联接获得的。
参数类型:
TOuter 第一个序列中的元素的类型。 TInner 第二个序列中的元素的类型。 TKey 键选择器函数返回的键的类型。 TResult 结果元素的类型。
参考链接如下:
https://msdn.microsoft.com/zh-cn/library/bb534297.aspx
https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.groupjoin?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(System.Linq.Enumerable.GroupJoin%60%604);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.7.1
例程:
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 = 18 };
Person p2 = new Person() { Name = "EFG", Age = 19 };
Person p3 = new Person() { Name = "LMN", Age = 20 };
Person p4 = new Person() { Name = "XYZ", Age = 21 };
List pList = new List { 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 dList = new List { 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}";
}
}
} 输出结果:

以上是“C#中如何使用Join与GroupJoin将两个集合进行关联与分组”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站制作公司行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:C#中如何使用Join与GroupJoin将两个集合进行关联与分组-创新互联
分享链接:http://www.scyingshan.cn/article/cchhsp.html


咨询
建站咨询
