本文为原创文章、源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称、作者及网址,谢谢!


本文使用的开发环境是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#]使用GroupJoin将两个关联的集合进行分组的更多相关文章

  1. Oracle中如何实现Mysql的两表关联update操作

    在看<MySQL 5.1参考手册>的时候,发现MySQL提供了一种两表关联update操作.原文如下: UPDATE items,month SET items.price=month.p ...

  2. select两个关联的下拉列表

    今天用到两个关联的select,整理一下代码,仅供参考 如下: <html> <head> <meta charset="UTF-8"> < ...

  3. Python selenium 一个节点两个关联input

    HTML代码: 一个节点两个关联input  多出现于密码框 先需要模拟点击进入第一个input,才能激活第二个input. 代码: driver.find_element_by_name('Text ...

  4. oracle 两表关联查询

      oracle 两表关联查询 CreationTime--2018年7月4日17点27分 Author:Marydon 情景描述 查询学生表student,sname,sex,age信息及所在班级c ...

  5. Oracle两表关联,只取B表的第一条记录

    背景:  A表.B表两表关联,关联出来的结果里B表有不止一条,需求是只要B表结果中的某一条(按某字段排序) 首先想到了直接写个带排序的子查询去匹配外围的值,从这个结果集中只要第一条,但是经过验证发现, ...

  6. MyBatis 中两表关联查询MYSQL (14)

    MyBatis 中两表关联查询MYSQL 1.创建数据库表语句 2.插入测试数据 3.pom文件内容 <?xml version="1.0" encoding="U ...

  7. Oracle-left join两表关联只取B表匹配到的第一条记录【over partition by(分组后对组内数据排序)】

    背景:  A表.B表两表关联,关联出来的结果里B表有不止一条,需求是只要B表结果中的某一条(按某字段排序) 经过百度,发现 row_number() over(partition by a order ...

  8. PHP明细之间的关联和having进行分组,不推荐这样做,只是做为偷懒的办法

    -- 只求和wrt的数据,其它数据保持不变!SELECT A.return_id,A.relevant_id,A.order_id,A.deliver_order_id,A.product_id,A. ...

  9. Android ORMLite ForeignCollection关联外部集合

     <Android ORMLite ForeignCollection关联外部集合>    Android ORMLite ForeignCollection关联外部集合的功能,适合层 ...

随机推荐

  1. 《剑指Offer》面试题5-替换空格

    题目:请实现一个函数,把字符串中的每个空格替换成"%20".例如输入"We are happy.",则输出"We%20are%20happy.&quo ...

  2. oracle 表空间不足解决办法

    问题:在对某一表空间进行新建表的时候,出现ora-01658的错误. create 语句: create table OA_ORGCONFIG(  OAOC_UNID      INTEGER not ...

  3. 深入理解Java虚拟机--上

    深入理解Java虚拟机--上 第2章 Java内存区域和内存溢出异常 2.2 运行时数据区域 图 2-1 Java虚拟机运行时数据区 2.2.1 程序计数器 程序计数器可以看作是当前线程所执行的字节码 ...

  4. spring 学习笔记1

    Spring 学习记录 任何一个成功的应用都是由多个为了实现某一个业务目标而相互协作的组件构成的.这些组件必须彼此了解,并相互协作来完成工作. 在Spring 中,对象无需自己负责查找或创建与其关联的 ...

  5. 我只是想获取access_token而已

    起因是想在微信小程序中获取access_token. 之前资源只有一个阿里云虚拟主机和一个域名,于是用C#后端写了GET请求的接口,准备调用自己域名下的接口获取access_token 使用微信的wx ...

  6. js 短信倒计时60s

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. input file选择图片后 预览

    很多前端都选择用插件来实现图片预览,这个小功能也可以很简单的用jQuery来实现 简单的jQuery实现input file选择图片后,可以预览图片的效果 简单的HTML代码: <div> ...

  8. Problem J

    Problem Description 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是 ...

  9. node调试工具--node-inspector安装

    node-inspector安装: npm install --registry=http://r.cnpmjs.org -g cnpm cnpm install -g node-inspector ...

  10. day3--深入学习命令总结

    1.查看命令帮助的几种方法 a.[命令] --help   适用于一般命令,非内置命令 b.man  [命令]     适用于一般命令,非内置命令 c.help  [命令]     适用于内置命令 d ...