我有两个类,一个嵌套在另一个类中。
Public Class Operation
Public Property Name
Public Property Records As List(Of Record)
Public Class Record
Public Property ID as Integer
现在,我有一系列操作 - >
Dim pOperations as List(Of Operation)
“名称”或“ID”在操作和记录之间都不是唯一的。
我希望使用LINQ = Dictionary(Of String,Of List(Of Integer))构建一个字典,其中键是我的集合中名称的唯一示例,值是与这些名称相关联的不同ID的集合集合名。
答案 0 :(得分:2)
这会创建一个Dictionary(Of String, List(Of Int32))
,其唯一名称为键,并且将Record-ID作为值关联。
Dim dict = (From op In pOperations
From rec In op.Records
Group rec By op.Name Into NameGrp = Group) _
.ToDictionary(Function(grp) (grp.Name),
Function(grp) (grp.NameGrp.Select(Function(r) (r.ID)).ToList))
答案 1 :(得分:1)
Dim dict = (From op In pOperations _
From rec In op.Records _
Group rec By op.Name Into NameGrp = Group) _
.ToDictionary(Function(grp) (grp.Name), _
Function(grp) (grp.NameGrp.Select(Function(r) _
(r.ID)).Distinct().ToList))