欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-19 11:31:04
本文使用的开发环境是vs2017及dotnet4.0,写此随笔的目的是给自己及新开发人员作为参考, 对于join的用法说明如下: 语法: public sta...

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

对于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
)

参数说明:

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 通过对两个序列执行内部联接获得的。

参数类型:

touter
第一个序列中的元素的类型。
tinner
第二个序列中的元素的类型。
tkey
键选择器函数返回的键的类型。
tresult
结果元素的类型。

参考链接如下:


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

输出结果:

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

对于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
)

参数说明:

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, ienumerable<tinner>, tresult>

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

返回值

type: system.collections.generic.ienumerable<tresult>
ienumerable<t> ,其中包含类型的元素 tresult 通过对两个序列执行分组的联接获得的。

参数类型:

touter
第一个序列中的元素的类型。
tinner
第二个序列中的元素的类型。
tkey
键选择器函数返回的键的类型。
tresult
结果元素的类型。

参考链接如下:


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

输出结果:

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

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

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

总结

以上所述是小编给大家介绍的c#中使用join与groupjoin将两个集合进行关联与分组,希望对大家有所帮助

上一篇:

下一篇: