D_Bester
D_Bester

Reputation: 5921

Count from List(of List(of myObject))

I have a list of lists (StoreList). I need a count of all available apples across all stores. Not sure how to do that.

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

Obviously I can't sum a count the way I'm doing it. How would I use Linq to get a count of all available apples across all stores?

Upvotes: 0

Views: 50

Answers (1)

Prisoner
Prisoner

Reputation: 1857

You have forgot to assign the function in the Sum try this:

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

Upvotes: 2

Related Questions