嵌套排序列表vb.net中的分组



我有一个对象列表。

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)

知道如何按CustomerName和ItemName对它们进行分组并将它们排列到嵌套的排序列表中吗?

我想将它们排列到的数据类型如下:-

Dim collection As SortedList(Of String, SortedList(Of String, List(Of Sales)))

外部sortedList中的关键字将是CustomerName。

内部sortedList中的键将是ItemName。

非常感谢你的帮助!

很简单:

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()))))

最新更新