Tom Tom - 1 year ago 77 Question

VB.NET LINQ - Count of collection within collection?

Take the following scenario:

Public Class Store
Public Overridable Property Areas As List(Of Area)
End Class

Public Class Area
Public Overridable Property Shelves As List(Of Shelf)
End Class

Public Class Shelf
Public Property ID as integer
End Class

What's the quickest way to get a total count of shelves for a store?
I.e. for a store I get the total count of areas by using Areas.Count

Or will I need to loop through each area and tally the count of shelves?

Answer Source

In C#:

int count = Store.Areas.SelectMany(x => x.Shelves).Count();

Converted to VB:

Dim count = Store.Areas.SelectMany(Function(x) x.Shelves).Count()

(using an online converter, not a VB guy)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download