Como convertir un IList a Datatable

By | 2012/03/23

Trabajando en una aplicación que me permite sincronizar información con uno de mis clientes me tope con la necesidad de transformar un IList que era entregado por el cliente a un DataTable.
Asi quedo todo:

Public Shared Function IList_DataTable(Of T)(ByVal list As IList(Of T)) As DataTable
Dim td As New DataTable
Dim entityType As Type = GetType(T)
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(entityType)

For Each prop As PropertyDescriptor In properties
td.Columns.Add(prop.Name)
Next

For Each item As T In list
Dim row As DataRow = td.NewRow()

For Each prop As PropertyDescriptor In properties
row(prop.Name) = prop.GetValue(item)
Next

td.Rows.Add(row)
Next

Return table
End Function

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.