Select 和 SelectMany 的区别

编程入门 行业动态 更新时间:2024-10-25 14:33:12
本文介绍了Select 和 SelectMany 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在寻找 Select 和 SelectMany 之间的区别,但一直找不到合适的答案.我需要了解使用 LINQ To SQL 时的区别,但我发现的只是标准数组示例.

I've been searching the difference between Select and SelectMany but I haven't been able to find a suitable answer. I need to learn the difference when using LINQ To SQL but all I've found are standard array examples.

谁能提供一个 LINQ To SQL 示例?

Can someone provide a LINQ To SQL example?

推荐答案

SelectMany 扁平化返回列表列表的查询.例如

SelectMany flattens queries that return lists of lists. For example

public class PhoneNumber { public string Number { get; set; } } public class Person { public IEnumerable<PhoneNumber> PhoneNumbers { get; set; } public string Name { get; set; } } IEnumerable<Person> people = new List<Person>(); // Select gets a list of lists of phone numbers IEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers); // SelectMany flattens it to just a list of phone numbers. IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers); // And to include data from the parent in the result: // pass an expression to the second parameter (resultSelector) in the overload: var directory = people .SelectMany(p => p.PhoneNumbers, (parent, child) => new { parent.Name, child.Number });

.NET Fiddle 现场演示

更多推荐

Select 和 SelectMany 的区别

本文发布于:2023-11-06 09:47:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   Select   SelectMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!