欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文使用的開發(fā)環(huán)境是VS2017及dotNet4.0,寫此隨筆的目的是給自己及新開發(fā)人員作為參考,

對(duì)于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
)

參數(shù)說明:

outer
Type: System.Collections.Generic.IEnumerable<TOuter>
要聯(lián)接的第一個(gè)序列。
inner
Type: System.Collections.Generic.IEnumerable<TInner>
要與第一個(gè)序列聯(lián)接的序列。
outerKeySelector
Type: System.Func<TOuter, TKey>
用于從第一個(gè)序列的每個(gè)元素提取聯(lián)接鍵的函數(shù)。
innerKeySelector
Type: System.Func<TInner, TKey>
用于從第二個(gè)序列的每個(gè)元素提取聯(lián)接鍵的函數(shù)。
resultSelector
Type: System.Func<TOuter, TInner, TResult>
用于從兩個(gè)匹配元素創(chuàng)建結(jié)果元素的函數(shù)。
返回值
Type: System.Collections.Generic.IEnumerable<TResult>
IEnumerable<T> ,其類型的元素 TResult 通過對(duì)兩個(gè)序列執(zhí)行內(nèi)部聯(lián)接獲得的。

參數(shù)類型:

TOuter
第一個(gè)序列中的元素的類型。
TInner
第二個(gè)序列中的元素的類型。
TKey
鍵選擇器函數(shù)返回的鍵的類型。
TResult
結(jié)果元素的類型。

參考鏈接如下:

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<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}";
 }
 }
}

輸出結(jié)果:

對(duì)于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
)

參數(shù)說明:

outer
Type: System.Collections.Generic.IEnumerable<TOuter>

要聯(lián)接的第一個(gè)序列。

inner
Type: System.Collections.Generic.IEnumerable<TInner>

要與第一個(gè)序列聯(lián)接的序列。

outerKeySelector
Type: System.Func<TOuter, TKey>

用于從第一個(gè)序列的每個(gè)元素提取聯(lián)接鍵的函數(shù)。

innerKeySelector
Type: System.Func<TInner, TKey>

用于從第二個(gè)序列的每個(gè)元素提取聯(lián)接鍵的函數(shù)。

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

用于從第一個(gè)序列的元素和第二個(gè)序列的匹配元素集合中創(chuàng)建結(jié)果元素的函數(shù)。

返回值

Type: System.Collections.Generic.IEnumerable<TResult>
IEnumerable<T> ,其中包含類型的元素 TResult 通過對(duì)兩個(gè)序列執(zhí)行分組的聯(lián)接獲得的。

參數(shù)類型:

TOuter
第一個(gè)序列中的元素的類型。
TInner
第二個(gè)序列中的元素的類型。
TKey
鍵選擇器函數(shù)返回的鍵的類型。
TResult
結(jié)果元素的類型。

參考鏈接如下:

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<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}";
 }
 }
}

輸出結(jié)果:

以上代碼僅在Join與GroupJoin最后一個(gè)參數(shù)有區(qū)別,可以參見紅色字體部分,

并從以上結(jié)果來看,Join與GroupJoin的區(qū)別一個(gè)在于:Join僅僅是將兩個(gè)結(jié)合進(jìn)行關(guān)聯(lián),而GroupJoin則會(huì)進(jìn)行分組。

總結(jié)

以上所述是小編給大家介紹的C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!

上一篇:C#程序員統(tǒng)計(jì)自己的代碼行數(shù)

欄    目:C#教程

下一篇:C#中字符串優(yōu)化String.Intern、IsInterned詳解

本文標(biāo)題:C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5355.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有