如何聚合T的多个IEnumebles

Ror*_*ker 18 .net linq vb.net ienumerable

鉴于....

Public MasterList as IEnumerable(Of MasterItem)
Public Class MasterItem(Of T) 
    Public SubItems as IEnumerable(Of T)
End Class 
Run Code Online (Sandbox Code Playgroud)

我想要一个IEnumerable(Of T),它将遍历MasterList中所有MasterItem的所有SubItems

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

Per*_*ahl 27

你在寻找SelectMany()吗?

MasterList.SelectMany(master => master.SubItems)
Run Code Online (Sandbox Code Playgroud)

对不起C#,不懂VB.

  • FWIW VB.Net版本是"MasterList.SelectMany(function(master)master.SubItems)" (2认同)

Oli*_*ppi 8

您可以通过Linq与SelectMany实现此目的

C#代码

masterLists.SelectMany(l => l.SubItems);
Run Code Online (Sandbox Code Playgroud)


最好的祝福


yfe*_*lum 8

Enumerable.SelectManyIEnumerablemonad 的关键,就像它的Haskell等价物一样concatMap,是Haskell列表monad的关键.

事实证明,你的问题正好涉及计算机科学深层次的核心.

你需要小心你的措辞,因为这Aggregate意味着一些非常不同的东西SelectMany- 甚至相反.AggregateIEnumerable值组合成单个值(可能是另一种类型),而SelectMany unIEnumerable值组合成更多值(可能是另一种类型).