Select和SelectMany之间的区别

Select和SelectMany之间的区别,第1张

Select和SelectMany之间的区别

SelectMany
展平返回列表列表的查询。例如

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 numbersIEnumerable<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上的实时演示



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5602328.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存