我有一个列表列表(StoreList)。我需要在所有商店中统计所有可用的苹果。不知道怎么做。
Class myObject
Public What As String
Public Available As Boolean
End Class
Public Sub Test()
Dim ItemList As New List(Of myObject)
ItemList.Add(New myObject With {.What = "Apple", .Available = True})
ItemList.Add(New myObject With {.What = "Cherry", .Available = False})
Dim StoreList As New List(Of List(Of myObject))
StoreList.Add(ItemList)
'StoreList.Add(...)
'error here
Dim count As Integer = StoreList.Sum( _
ItemList.Where(Function(x As myObject) _
x.What = "Apple" And x.Available).Count)
End Sub
显然,我无法按照我的方式来计算。我如何使用Linq来计算所有商店中所有可用苹果的数量?
答案 0 :(得分:2)
您忘记在Sum
中分配该功能
试试这个:
Dim count As Integer = StoreList.Sum( Function(inner) _
inner.Where(Function(x As myObject) _
x.What = "Apple" And x.Available).Count)