首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何聚合T的多个IEnumerables

如何聚合T的多个IEnumerables
EN

Stack Overflow用户
提问于 2009-06-30 11:47:39
回答 4查看 4.2K关注 0票数 18

给定..。

代码语言:javascript
复制
Public MasterList as IEnumerable(Of MasterItem)
Public Class MasterItem(Of T) 
    Public SubItems as IEnumerable(Of T)
End Class 

我想要一个单独的IEnumerable(Of T),它将迭代通过MasterList中所有MasterItems的所有SubItems

我认为有一个Linq工具可以做到这一点,或者我忽略了一个扩展方法。我需要一种在VB9 (2008)中工作的机制,因此不使用Yield。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-06-30 11:50:59

你在找SelectMany()吗?

代码语言:javascript
复制
MasterList.SelectMany(master => master.SubItems)

对不起,C#,我不懂VB。

票数 27
EN

Stack Overflow用户

发布于 2009-06-30 11:51:16

你可以通过Linq和SelectMany实现这一点。

C#代码

代码语言:javascript
复制
masterLists.SelectMany(l => l.SubItems);

诚挚的问候

票数 8
EN

Stack Overflow用户

发布于 2014-02-05 09:44:16

只是为了提供真正的VB.NET答案:

代码语言:javascript
复制
' Identical to Per Erik Stendahl's and Oliver Hanappi's C# answers
Dim children1 = MasterList.SelectMany(Function(master) master.SubItems)

' Using VB.NET query syntax
Dim children2 = From master In MasterList, child in master.SubItems Select child

' Using Aggregate, as the question title referred to
Dim children3 = Aggregate master In MasterList Into SelectMany(master.SubItems)

除了children2需要Function(master, child) child的等价物之外,所有这些都被编译成相同的IL。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1063234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档