惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
D
Docker
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
D
DataBreaches.Net
月光博客
月光博客
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
MyScale Blog
MyScale Blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学

博客园 - hrx521

微信公众号H5开发踩坑记:为什么POST返回的页面会显示HTML源码 SQL NOT IN 子查询存在 NULL 的经典行为陷阱--NULL 三值逻辑(TRUE / FALSE / UNKNOWN) SQL用窗口函数做逐行累计的实用用例 我的蝶翼斑马鱼繁殖要领总结 如何从操作系统的角度,理解C#语言的Async Await 异步机制内部状态机的运行原理? C#扩展方法对软件工程构建的促进示例和总结 基于原数据库新创建一个带部分数据做为初始数据的数据库的思路 sql调优记录,不要在join on 条件中使用过多的条件 发布和更新自己的nuget包 Rust的枚举类型Enum Rust语言特色语法记录 Rustup-init.exe安装后执行cargo run 报错:`link.exe` returned an unexpected error的解决办法 FastReport.OpenSource .Net下开源免费报表打印组件 SQL使用Merge在一个语句中完成插入、更新和删除操作 IIS应用程序回收导致应用中Hangfire等后台任务无法正常启动工作的解决方法 Microsoft.AspNetCore.Identity 的使用记录 C# xml文档反序列化记事 债券与债券基金 Blazor学习记录_12._IIS部署_组件的引用_HTML元素的引用 Blazor学习记录_11.身份认证与授权 Blazor学习记录_10.C#和JS互操作_访问WEB API Blazor学习记录_9.预呈现_渲染树
Linq补充学习
hrx521 · 2024-04-16 · via 博客园 - hrx521

1.Select 和 SelectMany 方法

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
将序列的每个元素投影到 IEnumerable 并将结果序列合并为一个序列。

1. 使用 SelectMany<TSource,TResult>(IEnumerable, Func<TSource,IEnumerable>) 对数组执行一对多投影。

    PetOwner[] petOwners =
        { new PetOwner { Name="Higa, Sidney",
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi, Ronen",
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price, Vernette",
              Pets = new List<string>{ "Scratches", "Diesel" } } };

    // 使用 SelectMany() 查询
    IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets);
    // 使用 Select() 查询达到使用 SelectMany() 的查询效果
    IEnumerable<List<String>> query2 =
        petOwners.Select(petOwner => petOwner.Pets);

2.将序列的每个元素投影到 IEnumerable<T>,并将结果序列合并为一个序列,并对其中每个元素调用结果选择器函数。

方法:SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
原型:

public static System.Collections.Generic.IEnumerable<TResult> 
SelectMany<TSource,TCollection,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, 
Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector, 
Func<TSource,TCollection,TResult> resultSelector);

示例:

//将有1对多关系的两个集合,合并输出为一个集合
var query3 = petOwners.SelectMany(petOwner => petOwner.Pets, (owner, pet) => new { OwnerName = owner.Name, Pet = pet });
query3.ToList().ForEach(x => Console.WriteLine($"{x.OwnerName}:{x.Pet}"));