区别与选择之间的SelectMany [英] Difference Between Select and SelectMany

查看:115
本文介绍了区别与选择之间的SelectMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找选择的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?

推荐答案

<一类=文档链接 href=\"http://stackoverflow.com/documentation/c%23/68/linq-queries/347/selectmany#t=201608121045348689229\"><$c$c>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 });

请参阅<一类=文档链接 href=\"http://stackoverflow.com/documentation/c%23/68/linq-queries/347/selectmany#t=201608121045348689229\">SelectMany - StackOverflow的文档

现场演示.NET的小提琴

这篇关于区别与选择之间的SelectMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆