一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
主站蜘蛛池模板: 91精品国产亚洲爽啪在线影院 | 火影小南被爆羞羞网站进入 | 韩剧网3600热播剧 | 亚洲精品第五页 | 国产盗摄美女嘘嘘视频 | 天美传媒tm0065 | 日本欧美一二三区色视频 | 国产自拍视频网站 | 女暴露狂校园裸露小说 | 极品蜜桃臀美女啪啪 | 久久视频在线视频观看天天看视频 | 视频免费看 | 日韩在线视频免费观看 | 国产欧美久久一区二区 | 问一问免费咨询 | 国产成人精品免费视频软件 | 91久久青青草原线免费 | 视频在线网站 | 午夜AV亚洲一码二中文字幕青青 | 美女被吸乳老师羞羞漫画 | 色怡红院 | 国产精品久久久99 | 扒开斗罗美女了的胸罩和内裤漫画 | 久久re热在线视频精69 | 精品在线播放视频 | 色人阁小说 | 国产女王女m视频vk 国产农村一级特黄α真人毛片 | 日韩精品成人免费观看 | 2022日韩理论片在线观看 | 国产精品久久久久久五月尺 | 美女操批 | 精品国产福利一区二区在线 | 九九热在线视频观看这里只有精品 | 亚洲国产99 | 91.久久| 女仆色网址 | 第一次出血videos | 精品网站一区二区三区网站 | 大象传媒1234区 | 日本中文字幕在线精品 | 91香蕉影院 |