Mike Cole
Mike Cole

Reputation: 14753

Flattening hierarchical data with Linq in VB.NET

Given the following structure:

Public Class Vendor
    Public Property Accounts As Account()
End Class

Public Class Account
    Public Property Services As Service()
End Class

Public Class Service
    Public Property Name As String
End Class

How do I get a flat list of all included Services in all accounts given a single Vendor? This is what I've tried so far:

vendor.Accounts.Select(Function(acct) acct.Services) 'Returns a collection of services collections

I know I'm just missing an obvious operator.

Upvotes: 2

Views: 963

Answers (1)

Jason Malinowski
Jason Malinowski

Reputation: 19031

You're looking for SelectMany.

vendor.Accounts.SelectMany(Function(acct) acct.Services)

And if you want only the unique ones, slap a .Distinct() on the end.

Upvotes: 3

Related Questions