How do I Aggregate multiple IEnumerables of T

后端 未结 5 1792
春和景丽
春和景丽 2021-02-12 18:20

Given....

Public MasterList as IEnumerable(Of MasterItem)
Public Class MasterItem(Of T) 
    Public SubItems as IEnumerable(Of T)
End Class 

I

5条回答
  •  自闭症患者
    2021-02-12 19:02

    Just to provide true VB.NET answers:

    ' 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)
    

    These all compile down to the same IL, except children2 requires the equivalent of Function(master, child) child.

提交回复
热议问题