C#中使用Join与GroupJoin将两个集合进行关联与分组

C#中使用Join与GroupJoin将两个集合进行关联与分组

2023年7月22日发(作者:)

C#中使⽤Join与GroupJoin将两个集合进⾏关联与分组本⽂使⽤的开发环境是VS2017及dotNet4.0,写此随笔的⽬的是给⾃⼰及新开发⼈员作为参考,对于Join的⽤法说明如下:语法:public static IEnumerable Join( this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector)参数说明:outerType: rable要联接的第⼀个序列。innerType: rable要与第⼀个序列联接的序列。outerKeySelectorType: ⽤于从第⼀个序列的每个元素提取联接键的函数。innerKeySelectorType: ⽤于从第⼆个序列的每个元素提取联接键的函数。resultSelectorType: ⽤于从两个匹配元素创建结果元素的函数。返回值Type: rableIEnumerable ,其类型的元素 TResult 通过对两个序列执⾏内部联接获得的。参数类型:TOuter第⼀个序列中的元素的类型。TInner第⼆个序列中的元素的类型。TKey键选择器函数返回的键的类型。TResult结果元素的类型。参考链接如下:例程:using System;using c;using ;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 = (dList, person => person, department => ee, (person, department) => new { Person = person, Department = department }); foreach(var item1 in result) { ($"Name:{} & Department:{ment} "); ine(); } } } 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 GroupJoin( this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector)参数说明:outerType: rable要联接的第⼀个序列。innerType: rable要与第⼀个序列联接的序列。outerKeySelectorType: ⽤于从第⼀个序列的每个元素提取联接键的函数。innerKeySelectorType: ⽤于从第⼆个序列的每个元素提取联接键的函数。resultSelectorType: , TResult>⽤于从第⼀个序列的元素和第⼆个序列的匹配元素集合中创建结果元素的函数。返回值Type: rableIEnumerable ,其中包含类型的元素 TResult 通过对两个序列执⾏分组的联接获得的。参数类型:TOuter第⼀个序列中的元素的类型。TInner第⼆个序列中的元素的类型。TKey键选择器函数返回的键的类型。TResult结果元素的类型。参考链接如下:例程:using System;using c;using ;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 = oin(dList, person => person, department => ee, (person, departments) => new { Person = person, Department = (d => d) }); foreach(var item1 in result) { ($"Name:{} & "); foreach(var item2 in ment) { if(() == item2) ($"Department:{item2} "); else ($"{item2} "); } ine(); } } } 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}"; } }}输出结果:以上代码仅在Join与GroupJoin最后⼀个参数有区别,可以参见红⾊字体部分,并从以上结果来看,Join与GroupJoin的区别⼀个在于:Join仅仅是将两个结合进⾏关联,⽽GroupJoin则会进⾏分组。总结以上所述是⼩编给⼤家介绍的C#中使⽤Join与GroupJoin将两个集合进⾏关联与分组,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689985884a298380.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信