jueves, 22 de mayo de 2014

How convert a list object to dataset -Como convertir una lista de objetos en dataset

  Public Function ListObjectToDataSet(Of T)(ByVal list As IList(Of T)) As DataSet
        Dim elementType As Type
        Dim ds As New DataSet()
        Dim t1 As New DataTable()
            elementType = GetType(T)
            'add a column to table for each public property on T
            For Each propInfo In elementType.GetProperties()
                Dim ColType As Type = If(Nullable.GetUnderlyingType(propInfo.PropertyType), propInfo.PropertyType)
                t1.Columns.Add(propInfo.Name, ColType)
            'go through each property on T and add each value to the table      
            For Each item As T In list
                Dim row As DataRow = t1.NewRow()
                For Each propInfo In elementType.GetProperties()
                    row(propInfo.Name) = If(propInfo.GetValue(item, Nothing), DBNull.Value)
            Return ds
        Catch ex As Exception
            Return Nothing
        End Try
    End Function