使用SelectMany()的不同方式 [英] Different ways of using SelectMany()

查看:61
本文介绍了使用SelectMany()的不同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用SelectMany().似乎有很多争论,根据我自己的研究,我注意到SelectMany()可能是所有其他选择操作的父亲".

I'd like to know how to use SelectMany(). It seems to take so many arguments and from my own research I noticed that SelectMany() might be the 'father' of all other select operations.

推荐答案

选择许多"允许您从查询源中选择一个IEnumerable< T>属性.集合,而不是返回集合(IEnumerable< IEnumerable< T>>>)的集合,而是将集合扁平化为单个集合.

Select many allows you to select a property from your query source that is an IEnumerable<T> collection, but instead of returning a collection of collections (IEnumerable<IEnumerable<T>>) it will flatten the collections into a single collection.

下面是一个示例,您可以运行该示例来演示Select和SelectMany之间的区别:

Here's an example that you can run to demonstrate the differences between Select and SelectMany:

//set up some data for our example
var tuple1 = new { Name = "Tuple1", Values = new int [] { 1, 2, 3 } };
var tuple2 = new { Name = "Tuple2", Values = new int [] { 4, 5, 6 } };
var tuple3 = new { Name = "Tuple3", Values = new int [] { 7, 8, 9 } };

//put the tuples into a collection
var tuples = new [] { tuple1, tuple2, tuple3 };

//"tupleValues" is an IEnumerable<IEnumerable<int>> that contains { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }
var tupleValues = tuples.Select(t => t.Values);

//"tupleSelectManyValues" is an IEnumerable<int> that contains { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
var tupleSelectManyValues = tuples.SelectMany(t => t.Values);

通过使用SelectMany,您可以更轻松地查询子集合中的值.

By using SelectMany you make it easier to query values within child collections.

这篇关于使用SelectMany()的不同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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