从列表中计数(列表(myObject))

时间:2016-10-04 01:33:49

标签: vb.net linq

我有一个列表列表(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来计算所有商店中所有可用苹果的数量?

1 个答案:

答案 0 :(得分:2)

您忘记在Sum中分配该功能 试试这个:

Dim count As Integer = StoreList.Sum( Function(inner) _
        inner.Where(Function(x As myObject) _
               x.What = "Apple" And x.Available).Count)