LINQ 之 SelectMany
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢!
一、第一种用法:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector);
官方释义:将序列的每个元素投影到 IEnumerable<TResult> 并将结果序列合并为一个序列。
废话不多说,直接Post上代码:
1,编写Person类:

class Person
{
public string Name { set; get; }
public int Age { set; get; }
public string Gender { set; get; }
public Dog[] Dogs { set; get; }
}

2,编写Dog类:
public class Dog
{
public string Name { set; get; }
}
请注意:在Person类里有一个Dog数组,用于存储实例化Person类所拥有的所有Dog集合,这里就是SelectMany的关键。
3、编写客户端试验代码:

List<Person> personList = new List<Person>
{
new Person
{
Name = "P1", Age = , Gender = "Male",
Gogs = new Dog[]
{
new Dog { Name = "D1" },
new Dog { Name = "D2" }
}
},
new Person
{
Name = "P2", Age = , Gender = "Male",
Gogs = new Dog[]
{
new Dog { Name = "D3" }
}
},
new Person
{
Name = "P3", Age = ,Gender = "Female",
Dogs = new Dog[]
{
new Dog { Name = "D4" },
new Dog { Name = "D5" },
new Dog { Name = "D6" }
}
}
};
var dogs = personList.SelectMany(p => p.Dogs);
foreach (var dog in dogs)
{
Console.WriteLine(dog.Name);
}

在这里我们定义了由Person构成的List列表,并使用集合及对象初始化器初始化了一些数据。
在这里,SelectMany的作用就是:将personList集合对象的每个元素(每个Person实例对象,如名为“P1”,“P2”,“P3”)
映射到每个Person类对应的Dog集合(如名为“P1”对应Dog名为D1及D2的Dog集合),
并将每个Person类对应Dog的集合重新组合成一个大的Dog集合。
因此,以上将会输出以下结果:

实际以上的SelectMany对应的LINQ语句为:
var dogs = from p in personList
from d in p.Dogs
select d;
我们可以将其代替试试就知道结果。
2、第二种用法:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector);
官方释义:将序列的每个元素投影到 IEnumerable<TResult>,并将结果序列合并为一个序列。每个源元素的索引用于该元素的投影表。
其实,就是比第一种使用方法多一个索引而已,该索引是从0开始,针对的是TSource指定类型的集合,最大索引值为TSource个数-1。
我们将第一种客户端试验代码中的
var dogs = personList.SelectMany(p => p.Dogs);
修改为
var dogs = personList.SelectMany((p, i) =>
p.Dogs.Select( d=>
{
d.Name = $"{i},{d.Name}";
return d;
}));
以上方法仅仅是把索引值加到Dog类的Name属性上。
由以上可以看到,共有3个Person,因此,索引值最大为2,每个Person类有多少个Dog(如名为P1的Person类,共有2个Dog),
对应的索引就被使用了多少次数(如名为P1的Person类,索引0被使用了2次),
输出结果如下:

三、第三种用法:
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector);
官方释义:将序列的每个元素投影到 IEnumerable<TCollection>,并将结果序列合并为一个序列,并对其中每个元素调用结果选择器函数。
这个用法,跟第一种用法相比,就是可以对已合成一个大集合的每个元素调用结果选择器,返回自己想要的集合类型。
编写客户端试验代码:

List<Person> personList = new List<Person>
{
new Person
{
Name = "P1", Age = , Gender = "Male",
Gogs = new Dog[]
{
new Dog { Name = "D1" },
new Dog { Name = "D2" }
}
},
new Person
{
Name = "P2", Age = , Gender = "Male",
Gogs = new Dog[]
{
new Dog { Name = "D3" }
}
},
new Person
{
Name = "P3", Age = ,Gender = "Female",
Gogs = new Dog[]
{
new Dog { Name = "D4" },
new Dog { Name = "D5" },
new Dog { Name = "D6" }
}
}
};
var results = personList.SelectMany(p => p.Dogs, (p, d) => new { PersonName = p.Name, DogName = d.Name });
foreach (var result in results)
{
Console.WriteLine($"{result.PersonName},{result.DogName}");
}

关于SelectMany的用法说明如下:
第一个参数:p=>p.Dogs,p指定是想要处理的每个Person对象,而p.Dogs则是想让p实例映射的Dog集合;
第二个参数:(p, d) => new { PersonName = p.Name, DogName = d.Name },p与d分别指定是映射后(其实有点类似数据库的CROSS JOIN)的person实例与dog实例,
如名为P1的Person类,其Dogs名为D1及D2,那么p与d就是:P1/D1,P1/D2(指定是名称),处理其他Person类也是如此。而new { PersonName = p.Name, DogName = d.Name }则是返回的一个由自己定义的匿名类型。
结果输出如下:

实际以上的SelectMany对应的LINQ语句为:
var results = from p in personList
from d in p.Dogs
select new { PersonName = p.Name, DogName = d.Name };
四、第四种用法:
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector);
官方释义:将序列的每个元素投影到 IEnumerable<TCollection>,并将结果序列合并为一个序列,并对其中每个元素调用结果选择器函数。每个源元素的索引用于该元素的中间投影表。
其实,就是比第三种使用方法多一个索引而已,该索引是从0开始,针对的是TSource指定类型的集合,最大索引值为TSource个数-1。
我们将第三种客户端试验代码中的
var results = personList.SelectMany(p => p.Dogs, (p, d) => new { PersonName = p.Name, DogName = d.Name });
修改为

var results = personList.SelectMany((p,i) =>
{
for(int j=;j<p.Dogs.Length;j++)
{
p.Dogs[j].Name = $"{i}-{p.Dogs[j].Name}";
}
return p.Dogs;
}, (p, d) => new { PersonName = p.Name, DogName = d.Name });

以上方法仅仅是把索引值加到Dog类的Name属性上,并将Dog集合返回。
由以上可以看到,共有3个Person,因此,索引值最大为2,每个Person类有多少个Dog(如名为P1的Person类,共有2个Dog),
对应的索引就被使用了多少次数(如名为P1的Person类,索引0被使用了2次),
输出结果如下:

LINQ 之 SelectMany的更多相关文章
- [C#] LINQ之SelectMany
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.第一种用法: public static IEnumerable<TResult> SelectMany<TSo ...
- [C#] LINQ之SelectMany和GroupJoin
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 记一次使用 SelectMany 的经历
最近在改造一个功能时为了减少循环的层数,于是想着将List列表映射为一个能直接使用颗粒大小的List列表,这样一层循环就可以解决问题. public class ConflictWordIte ...
- Entity Framework 与 面向对象
说要分享,我了个*,写了一半放草稿箱了两个星期都快发霉了,趁着周末写完发出来吧. 文章分为五部分: 基础.类讲述的是用到的一些EF与面向对象的基础: 业务是讲怎么划分设计业务: 设计模式和工作模式讲述 ...
- 并发编程概述--C#并发编程经典实例
优秀软件的一个关键特征就是具有并发性.过去的几十年,我们可以进行并发编程,但是难度很大.以前,并发性软件的编写.调试和维护都很难,这导致很多开发人员为图省事放弃了并发编程.新版.NET 中的程序库和语 ...
- linq里的select和selectmany操作
Select() 和 SelectMany() 的工作都是依据源值生成一个或多个结果值.Select() 为每个源值生成一个结果值.因此,总体结果是一个与源集合具有相同元素数目的集合.与之相反,Sel ...
- linq读书笔记3-操作符之select与selectmany
linq对数据的查询方式的表达形式主要有两种: var demo =from p in pList where p.id=*** select p; var demo =pList.where(p=& ...
- Linq常用List操作总结,ForEach、分页、交并集、去重、SelectMany等
/* 以下围绕Person类实现,Person类只有Name和Age两个属性 一.List<T>排序 1.1 List<T>提供了很多排序方法,sort(),Orderby() ...
- LINQ操作符二:SelectMany
SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列. 示例: student类: using System; using ...
随机推荐
- Java 8,Jenkins,Jacoco和Sonar进行持续集成
技术环境 在以安全与质量为主要驱动力的项目中,CI至关重要. 因此,我从我的团队开始进行"概念验证",以表明以下技术已准备好协同工作: Java 8, NetBeans 8.0 & ...
- Mysql优化之Explain查询计划查看
我们经常说到mysql优化,优化中一种常见的方式就是对于经常查询的字段创建索引.那么mysql中有哪些索引类型呢? 一.索引分类1.普通索引:即一个索引只包含单个列,一个表可以有多个单列索引 2.唯一 ...
- Python GUI开发,效率提升10倍的方法!
1 框架简介 这个框架的名字叫 PySimpleGUI,它完全基于Python语言,能非常方便地开发GUI界面,代码量相比现有框架减少50%到90%.并且,它提供了极为友好的Python风格的接口,大 ...
- PlayJava Day004
今日所学: /* 2019.08.19开始学习,此为补档. */ JDK 1.6:byte , int , short , char , enum JDK 1.7:byte , int , short ...
- jdk api 1.6,1.7,1.8,1.9版本(中文)
有需要的朋友,请自行到百度云下载 链接:https://pan.baidu.com/s/18WgEZ1WpBz5YexbbgikJcA 提取码:xry4
- sed文本处理
1.基本概述 sed是一个流编辑器, 非交互式的编辑器,它一次处理一行内容. 处理时,把当前处理的行存储在临时缓冲区中,称* 为"模式空间"(pattern space) 接着用 ...
- C# vs2017创建Com组件,并注册
1.创建一个普通类库dll项目,如:MyCom. 2.导出接口,添加Guid,Guid为全局唯一标识,可以用VS2017自带工具获取.获取Guid的方法,如图: (1)打开自带Guid工具. (2)首 ...
- SQLMAP之tamper详解
sqlmap 是一款注入神器广为人知,里面的 tamper 常常用来绕过 WAF ,很实用的模块,但是却常常被新手忽略(比如我),今天就整理总结一下 tamper 的用法以及 tamper 的编写 P ...
- Linux根目录下各目录含义
/boot:系统启动的相关文件,比如内核,grub /etc:配置文件 /dev:设备文件 /root:root用户的家目录 /home:用户家目录 /lib:库文件 /bin:用户的命令文件 /sb ...
- Linux中的硬链接和软链接的概念、区别及用法
概念: 硬链接(hard link): A是B的硬链接(A和B都是文件名),则A的目录项中的inode节点号与B的目录项中的inode节点号相同,即一个inode节点对应两个不同的文件名,两个文件名指 ...