[C#]使用Join与GroupJoin将两个集合进行关联与分组
本文为原创文章、源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称、作者及网址,谢谢!
本文使用的开发环境是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://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 = };
Person p2 = new Person() { Name = "EFG", Age = };
Person p3 = new Person() { Name = "LMN", Age = };
Person p4 = new Person() { Name = "XYZ", Age = }; 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}";
}
}
}
输出结果:

对于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://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 = };
Person p2 = new Person() { Name = "EFG", Age = };
Person p3 = new Person() { Name = "LMN", Age = };
Person p4 = new Person() { Name = "XYZ", Age = }; 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}";
}
}
}
输出结果:

以上代码仅在Join与GroupJoin最后一个参数有区别,可以参见红色字体部分,
并从以上结果来看,Join与GroupJoin的区别一个在于:Join仅仅是将两个结合进行关联,而GroupJoin则会进行分组。
[C#]使用Join与GroupJoin将两个集合进行关联与分组的更多相关文章
- C#中使用Join与GroupJoin将两个集合进行关联与分组
转自:https://www.cnblogs.com/cncc/p/7985843.html 对于Join的用法说明如下: 语法: public static IEnumerable<TResu ...
- [C#]使用GroupJoin将两个关联的集合进行分组
本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 本文使用的开发环境是VS2017及dotNet4.0,写此随笔的目的是给自己及新开发人员作为参 ...
- LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、
四.联接操作符 联接是指将一个数据源对象与另一个数据源对象进行关联或者联合的操作.这两个数据源对象通过一个共同的值或者属性进行关联. LINQ有两个联接操作符:Join和GroupJoin. 1. J ...
- [C#] LINQ之Join与GroupJoin
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.编写Person与City类,如下: class Person { public int CityID { set; get; } ...
- LINQ之 Join 与 GroupJoin
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.编写Person与City类,如下: class Person { public int CityID { set; get; } ...
- CROSS JOIN连接用于生成两张表的笛卡尔集
将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘 CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...
- Mysql两张表的关联字段不一致
工作中遇到了一个问题,邮件系统群发失败,后来经过排查查找到了原因 原来是因为mysql中的两张表的关联字段竟然不一致, 表A mysql> desc rm_user_router;+------ ...
- Oracle update 两表及以上关联更新,出现多值情况,不是一对一更新
为了方便起见,建立了以下简单模型,和构造了部分测试数据:在某个业务受理子系统BSS中, SQL 代码--客户资料表 create table customers ( customer_id numbe ...
- 求两个集合的交集和并集C#
我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...
随机推荐
- memcached编译安装报错 ,提示checking build system type... Invalid configuration `x86_64-unknown-linux-': machine `x86_64-unknown-linux' not recognized configure: error: /bin/sh ./config.sub x86_64-unknown-linu
- HDFS ErasureCode方案对比
HDFS目前存储文件的方案是将一个文件切分成多个Block进行存储,通常一个Block 64MB或者128MB,每个Block有多个副本(replica),每个副本作为一个整体存储在一个DataNod ...
- guid是否为空的判断
Guid类型的变量不会为空,初始化没有赋值的GUID应该是00000000-0000-0000-0000-000000000000 . 正确的判断应该是if(Guid testId== Guid.Em ...
- 陈远波(java)--Git 入门
本章节讲解思路:1.在Git hup官网注册一个Git账号:2.下载git bash管理工具 3.在git bash上绑定GitHup账号密码: 一:进入GitHup官网:https://githu ...
- SDN 第五次上机作业
1.搭建如下拓扑并连接控制器 2.下发相关流表和组表实现负载均衡 s1: s2: s3: s4: 3.抓包分析验证负载均衡 s4-eth1: s4-eth2: s4-eth3
- Django商城项目笔记No.10用户部分-登录接口
Django商城项目笔记No.10用户部分-登录接口 添加url路由 接下来第二步,增加返回内容: 增加结果如下: 配置:上边的方法定义了返回的内容都有哪些,那这个方法jwt还不知道,需要配置: 修改 ...
- Git提交分支
Git提交分支操作 1.git add 命令告诉 Git 开始对这些文件进行跟踪 git add . 2.然后提交 git commit -m'这是注释信息' 3.git pull命令用于从另一个存储 ...
- (转)Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding
基于python3.6.1版本,在一个.py文件中,加入这3行:import requests, re, sysreload(sys)sys.setdefaultencoding("utf- ...
- python第三十一课--递归(3.递归的弊端)
演示递归的弊端: def mySum(num): if num == 1: return 1 return num+mySum(num-1) mySum(998) [注意]:递归可以解决绝大多数循环能 ...
- Redis系列七:redis持久化
redis支持RDB和AOF两种持久化机制,持久化可以避免因进程退出而造成数据丢失 一.RDB持久化 RDB持久化把当前进程数据生成快照(.rdb)文件保存到硬盘的过程,有手动触发和自动触发 手动触发 ...