gpt4 book ai didi

vb.net - 如何用List(Of t)填充DataTable或将List(Of t)转换为DataTable?

转载 作者:行者123 更新时间:2023-12-04 13:12:34 29 4
gpt4 key购买 nike

我已经阅读了很多有关该主题的文章;其中最近一次是.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。

任何见解将不胜感激

最佳答案

您链接的代码假定成员被声明为属性。您没有声明属性。您可以使其与反射一起使用:

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

关于vb.net - 如何用List(Of t)填充DataTable或将List(Of t)转换为DataTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1805626/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com