D_Bester D_Bester - 2 months ago 19
Vb.net Question

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?

Answer

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)