给定..。
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。
发布于 2009-06-30 11:50:59
你在找SelectMany()吗?
MasterList.SelectMany(master => master.SubItems)
对不起,C#,我不懂VB。
发布于 2009-06-30 11:51:16
你可以通过Linq和SelectMany实现这一点。
C#代码
masterLists.SelectMany(l => l.SubItems);
诚挚的问候
发布于 2014-02-05 09:44:16
只是为了提供真正的VB.NET答案:
' 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。
https://stackoverflow.com/questions/1063234
复制相似问题