I have a list of objects.
Class Sales
Public Property CustomerName As String
Public ItemName As String
Public Quantity As Double
....other details...
End Class
Dim records As IList(Of Sales)
Any idea how I can group them by CustomerName, followed by ItemName and arrange them into a nested sorted list?
The data type that I want to arrange them to is as follow:-
Dim collection As SortedList(Of String, SortedList(Of String, List(Of Sales)))
The key in the outer sortedList would be CustomerName.
The key in the inner sortedList would be ItemName.
Thanks very much for your help!
It's simple:
Dim collection As New SortedList(Of String, SortedList(Of String, List(Of Sales)))( _
records _
.GroupBy(Function (r) r.CustomerName) _
.Select(Function (x) New With { .CustomerName = x.Key, .Items = x.GroupBy(Function (i) i.ItemName) }) _
.ToDictionary( _
Function (x) x.CustomerName, _
Function (x) New SortedList(Of String, List(Of Sales))(x.Items.ToDictionary(Function (y) y.Key, Function (y) y.ToList()))))