服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#中使用Join与GroupJoin将两个集合进行关联与分组

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

2022-02-13 15:47cnc C#

这篇文章主要介绍了C#中使用Join与GroupJoin将两个集合进行关联与分组,文中分别对Join和GroupJoin的用法进行详细说明,需要的朋友可以参考下

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

对于Join的用法说明如下:

语法:

?
1
2
3
4
5
6
7
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
)

参数说明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 通过对两个序列执行内部联接获得的。

参数类型:

?
1
2
3
4
5
6
7
8
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

例程:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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<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}";
 }
 }
}

输出结果:

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

对于GroupJoin的用法说明如下:

语法:

?
1
2
3
4
5
6
7
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
)

参数说明:

?
1
2
outer
Type: System.Collections.Generic.IEnumerable<TOuter>

要联接的第一个序列。

?
1
2
inner
Type: System.Collections.Generic.IEnumerable<TInner>

要与第一个序列联接的序列。

?
1
2
outerKeySelector
Type: System.Func<TOuter, TKey>

用于从第一个序列的每个元素提取联接键的函数。

?
1
2
innerKeySelector
Type: System.Func<TInner, TKey>

用于从第二个序列的每个元素提取联接键的函数。

?
1
2
resultSelector
Type: System.Func<TOuter, IEnumerable<TInner>, TResult>

用于从第一个序列的元素和第二个序列的匹配元素集合中创建结果元素的函数。

返回值

?
1
2
Type: System.Collections.Generic.IEnumerable<TResult>
IEnumerable<T> ,其中包含类型的元素 TResult 通过对两个序列执行分组的联接获得的。

参数类型:

?
1
2
3
4
5
6
7
8
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

例程:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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<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}";
 }
 }
}

输出结果:

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

以上代码仅在Join与GroupJoin最后一个参数有区别,可以参见红色字体部分,

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

总结

以上所述是小编给大家介绍的C#中使用Join与GroupJoin将两个集合进行关联与分组,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/cncc/p/7985843.html

延伸 · 阅读

精彩推荐
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08