Reputation: 171
I have read many posts on this topic; among them and most recently .NET - Convert Generic Collection to Data Table. Unfortunately, all to no avail.
I have a generic collection of structures :
Private Structure MyStruct
Dim sState as String
Dim lValue as Long
Dim iLayer as Integer
End Structure
Dim LOStates As New List(Of MyStruct)
I need to fill a DataTable with this list of structures but have no idea how to go about doing this. I am using vb.net in Visual Studio 2008.
Any insights will be greatly appreciated
Upvotes: 8
Views: 79533
Reputation: 175
Following @Hans Passant function if anyone is dealing with nullable types:
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
Values:
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
Upvotes: 0
Reputation: 51725
I have same issue than @SamSelikoff, moved to 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
Upvotes: 8
Reputation: 942358
The code you linked assumes the members are declared as properties. You didn't declare properties. You can make it work with Reflection:
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
Upvotes: 20