C#中使用Join与GroupJoin将两个集合进行关联与分组
作者:cnc 发布时间:2023-11-11 23:40:47
本文使用的开发环境是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 = 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}";
}
}
}
输出结果:
对于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 = 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}";
}
}
}
输出结果:
以上代码仅在Join与GroupJoin最后一个参数有区别,可以参见红色字体部分,
并从以上结果来看,Join与GroupJoin的区别一个在于:Join仅仅是将两个结合进行关联,而GroupJoin则会进行分组。
总结
以上所述是小编给大家介绍的C#中使用Join与GroupJoin将两个集合进行关联与分组网站的支持!
来源:http://www.cnblogs.com/cncc/p/7985843.html
猜你喜欢
- Spire.Cloud.SDK for .NET提供了接口PdfSecurityApi可用于加密、解密PDF文档。本文将通过C#代码演示具体
- 面向接口编程接口的定义及功能这里从java介入吧,在java中,接口是一种特殊的类,接口里面的量都是常量,接口的方法只有定义而没有实现,换句
- 基于Java swing+MySQL实现学生信息管理系统:主要实现JDBC对学生信息进行增删改查,应付一般课设足矣,分享给大家。(由于篇幅原
- 实践过程效果代码public partial class Frm_Libretto : Form{ public
- 本文实例讲述了C++实现的链表类。分享给大家供大家参考。具体如下:#include <iostream>using namesp
- Android 拦截返回键事件的实例详解KeyEvent类Android.View.KeyEvent类中定义了一系列的常量和方法,用来描述A
- 1、什么是值传递,什么是引用传递?值传递(pass by value)是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数
- spring data jpa @query返回map踩坑记录最近用spring data jpa,网上看的一些教程,有的说是返回的Obje
- 一 概述CMDD:\Project\Computer-Science-And-Technology\writeExam\farben\src
- 在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如w
- 本文实例为大家分享了Java生成6位随机字符串的实现代码,具体内容如下package com.amos.tools;import java.
- 前言前天工作中遇到了这样一个问题,我在接口的参数封装了一个pojo,这是很常见的,当参数一多,惯性的思维就是封装一个pojo.那么在参数前有
- 简单介绍一下Java中的Excel文件导出功能(基于HttpServletResponse实现下载)首先,引入需要依赖的jar包:<d
- 复合语句Java的复合语句是以整个区块为单位的语句,由{}以及{}内包含的内容组成对于复合语句来说,复合语句创建了一个局部变量的作用域,该作
- 1、什么是 IOC?IOC-Inversion of Control,即控制反转。它不是什么技术,而是一种设计思想。传统的创建对象的方法是直
- 冒泡排序原理:从头(左边)开始比较每一对相邻的元素,如果第1个比第2个大,就交换它们的位置,执行完一轮后,最末尾(最右边)就是最大的元素。举
- 服务器端代码:import java.io.BufferedReader; import java.io.InputStreamR
- 本文实例为大家分享了C#图像处理的具体代码,供大家参考,具体内容如下(1)在Form1窗体中的PictureBox1控件中显示通过OpenF
- java中的JsonSerializer用法背景今天在写项目的时候遇到一个问题,项目中有个类中有创建时间和更新时间两个属性,但是单位在后端用
- @ConditionalOnMissingBean,它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的