在 VB.NET 中将具有重复值的 DataTable 转换为 Dictionary(of String, List(of Object)) 的正确方法

Correct way to convert DataTable with duplicate values to Dictionary(of String, List(of Object)) in VB.NET

我正在尝试将数据table 转换为字典。 table格式如下:

ValueID | DependentValueID | DependencyDescription
__________________________________________________
A       | a                | some text
A       | b                | some text
B       | c                | some text
B       | d                | some text
B       | e                | some text
...

每个对象有 3 个属性:

  1. 值ID
  2. DependentValueID
  3. 依赖描述

词典格式如下:

(Key) | (Value)
_______________
A    |  {
     |    [ 
     |      ValueID = A
     |      DependentValueID = a
     |      DependencyDescription = "some text"
     |    ], 
     |    [ 
     |      ValueID = A
     |      DependentValueID = b
     |      DependencyDescription = "some text"
     |    ], 
     |  }
-----------------------------------------------
B    |  {
     |    [ 
     |      ValueID = B
     |      DependentValueID = c
     |      DependencyDescription = "some text"
     |    ], 
     |    [ 
     |      ValueID = B
     |      DependentValueID = d
     |      DependencyDescription = "some text"
     |    ], 
     |    [ 
     |      ValueID = B
     |      DependentValueID = e
     |      DependencyDescription = "some text"
     |    ] 
     |  }
...

我为此编写的代码:

Private Sub MySub()
    ' Declare variables and fill datatables   
    If DependenciesDataTable IsNot Nothing AndAlso DependenciesDataTable.Rows.Count > 0 Then
        For i As Integer = 0 To DependenciesDataTable.Rows.Count - 1
            DependenciesObject.ValueID = DependenciesDataTable.Rows(i).Item("ValueID").ToString
            DependenciesObject.DependentValueID = DependenciesDataTable.Rows(i).Item("DependentValueID").ToString
            DependenciesObject.DependencyDescription = DependenciesDataTable.Rows(i).Item("DependencyDescription").ToString

            If i > 0 Then
                If DependenciesDataTable.Rows(i).Item("ValueID").ToString.Equals(DependenciesDataTable.Rows(i - 1).Item("ValueID").ToString) Then
                    DependenciesObject.Add(DependenciesObject)
                Else
                    DependenciesDictionary.Add(DependenciesDataTable.Rows(i - 1).Item("ValueID").ToString, DependenciesObject)
                    DependenciesObject = New List(Of Object)
                    DependenciesObject.Add(DependenciesObject)
                End If
            ElseIf i = 0 Then
                DependenciesObject.Add(DependenciesObject)
            End If

            If i = DependenciesDataTable.Rows.Count - 1 Then
                DependenciesObject.Add(DependenciesObject)
                DependenciesDictionary.Add(DependenciesDataTable.Rows(i).Item("ValueID").ToString, DependenciesObject)
                DependenciesObject = New List(Of Object)
            End If
        Next
    End If
End Sub

虽然这可行,但我知道这是一个低于标准的解决方案,因为我是一名初级开发人员。我想要一些关于如何改进我处理问题的方法的建议。

我看到 dependenciesObjectList(Of Object),但为什么呢?使用 classes,或者甚至像 Tuple 这样更临时的东西......我的解决方案将使用 class 来保持你的依赖性

Public Class Dependency
    Public Property ValueID As String
    Public Property DependentValueID As String
    Public Property DependencyDescription As String
End Class

然后使用 LINQ,我们可以将 select 从 table 转换为 IEnumerable(Of Dependency)

Dim dependencies = DependenciesDataTable.Select().Select(Function(row) New Dependency With {.ValueID = row("ValueID"), .DependentValueID = row("DependentValueID"), .DependencyDescription = row("DependencyDescription")})

然后按 ValueID 属性 和 select 将结果分组到字典中

Dim dependenciesDictionary = dependencies.GroupBy(Function(d) d.ValueID).ToDictionary(Function(g) g.Key, Function(g) g.Select(Function(v) v).ToList())

你最终会得到 Dictionary(Of String, List(Of Dependency))

并打印出来以确保

For Each kvp In dependenciesDictionary
    Console.WriteLine($"Key: {kvp.Key}")
    For Each v In kvp.Value
        Console.WriteLine($"{vbTab}ValueID: {v.ValueID}, DependentValueID: {v.DependentValueID}, DependencyDescription: {v.DependencyDescription}")
    Next
Next

Key: A
ValueID: A, DependentValueID: a, DependencyDescription: some text
ValueID: A, DependentValueID: b, DependencyDescription: some text
Key: B
ValueID: B, DependentValueID: c, DependencyDescription: some text
ValueID: B, DependentValueID: d, DependencyDescription: some text
ValueID: B, DependentValueID: e, DependencyDescription: some text