【问题标题】:How to fill a DataTable with a List(Of t) or convert a List(Of t) to a DataTable?如何用 List(Of t) 填充 DataTable 或将 List(Of t) 转换为 DataTable?
【发布时间】:2009-11-26 21:04:46
【问题描述】:

我已经阅读了很多关于这个主题的帖子;其中包括最近的.NET - Convert Generic Collection to Data Table。不幸的是,一切都无济于事。

我有一个通用的结构集合:

Private Structure MyStruct
Dim sState as String
Dim lValue as Long
Dim iLayer as Integer
End Structure

Dim LOStates As New List(Of MyStruct)

我需要用这个结构列表填充 DataTable,但不知道如何去做。我在 Visual Studio 2008 中使用 vb.net。

任何见解将不胜感激

【问题讨论】:

    标签: vb.net datatable structure generic-list


    【解决方案1】:

    您链接的代码假定成员被声明为属性。你没有声明属性。你可以让它与反射一起工作:

    Imports System.Reflection
    ...
    
          Public Shared Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable
            Dim table As New DataTable()
            Dim fields() As FieldInfo = GetType(T).GetFields()
            For Each field As FieldInfo In fields
              table.Columns.Add(field.Name, field.FieldType)
            Next
            For Each item As T In list
              Dim row As DataRow = table.NewRow()
              For Each field As FieldInfo In fields
                row(field.Name) = field.GetValue(item)
              Next
              table.Rows.Add(row)
            Next
            Return table
          End Function
    

    【讨论】:

    • nobugz,感谢您的及时回复!我将此函数添加到我的类中,然后将结构列表 (oTable = ConvertToDataTable(LOStates)) 传递给它,但没有重新运行任何行 - 在返回到调用它的位置之前表计数 = 0。我想知道是否还有其他我遗漏或做错的事情......
    • 调试它。 for each 循环是否循环?表格有列吗?
    • 在调试中,我能够确定表计数 = 0。for each 循环确实循环。在执行返回表语句之前有 3 个列,但有 0 行。
    • 我注意到,在我输入完最后一个回复后,我就包含了 add 语句。但是在将第一行添加到 DataTable 之后,它会引用“System.Reflection.TargetInvocationException”,然后继续向 DataTable 添加更多行。
    • 对不起,让你这么痛心!非常感谢您的帮助和耐心:)
    【解决方案2】:

    我有与@SamSelikoff 相同的问题,已移至 GetProperties:

    Public Shared Function ConvertToDataTable(Of t)(
                                                      ByVal list As IList(Of t)
                                                   ) As DataTable
        Dim table As New DataTable()
        If Not list.Any Then
            'don't know schema ....
            Return table
        End If
        Dim fields() = list.First.GetType.GetProperties
        For Each field In fields
            table.Columns.Add(field.Name, field.PropertyType)
        Next
        For Each item In list
            Dim row As DataRow = table.NewRow()
            For Each field In fields
                dim p = item.GetType.GetProperty(field.Name)
                row(field.Name) = p.GetValue(item, Nothing)
            Next
            table.Rows.Add(row)
        Next
        Return table
    End Function
    

    【讨论】:

      【解决方案3】:

      如果有人正在处理可空类型,请遵循@Hans Passant 函数:

      For Each field As FieldInfo In fields
      ' Extra check for nullable
      If field.FieldType.AssemblyQualifiedName.Contains("System.Nullable") Then
          ' Insert proper type
          If field.FieldType.AssemblyQualifiedName.Contains("System.DateTime") Then
             table.Columns.Add(field.Name, Type.GetType("System.DateTime"))
          End If
      Else
          table.Columns.Add(field.Name, field.FieldType)
      End If
      Next
      

      价值观:

      For Each item As T In list
      Dim row As DataRow = table.NewRow()
      For Each field As FieldInfo In fields
          ' Check if value is null
          If field.GetValue(item) is nothing Then
              Continue For
          End If
          row(field.Name) = field.GetValue(item)
      Next
      table.Rows.Add(row)
      Next
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 2010-09-19
        • 2011-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多