我不确定确切的术语,但我尝试使用 LINQ 做的是用它的孩子的键“切换”字典的键。
这是我作为输入得到的集合:
Key:HeaderID Value:
Key:ItemID Value:Object
Key:ItemID Value:Object
Key:ItemID Value:Object
Key:HeaderID Value:
Key:ItemID Value:Object
Key:ItemID Value:Object
Key:ItemID Value:Object
Key:HeaderID Value:
Key:ItemID Value:Object
Key:ItemID Value:Object
Key:ItemID Value:Object
这是我想要作为输出的集合:
Key:ItemID Value:
Key:HeaderID Value:Object
Key:HeaderID Value:Object
Key:HeaderID Value:Object
Key:ItemID
Key:HeaderID Value:Object
Key:HeaderID Value:Object
Key:HeaderID Value:Object
Key:ItemID
Key:HeaderID Value:Object
Key:HeaderID Value:Object
Key:HeaderID Value:Object
我已经在 VB.net 中创建了代码:
Dim objInput As Dictionary(Of String, Dictionary(Of String, Object)) = fakefunction()
' ItemID -> HeaderID ColumnValue
Dim objOutput As New Dictionary(Of String, Dictionary(Of String, Object))
For Each strHeaderID As String In objInput.Keys
Dim objColumnValuesPerItem As Dictionary(Of String, Object) = objInput(strHeaderID)
For Each strItemID As String In objColumnValuesPerItem.Keys
Dim objColumnValue As Object = objColumnValuesPerItem(strItemID)
If Not objOutput.ContainsKey(strItemID) Then
objOutput.Add(strItemID, New Dictionary(Of String, Object))
End If
objOutput(strItemID).Add(strHeaderID, objColumnValue)
Next
Next
我问这个问题的原因是纯粹的教育,我曾尝试使用 LINQ 将其转换为正确的列表,但如果不创建非常复杂且冗长的 LINQ 语句,我将一事无成。