Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# convert list t to datatable

public static DataTable ToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }
Comment

convert list to datatable c#

Public 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
Comment

PREVIOUS NEXT
Code Example
Csharp :: How does works Unity interfaces 
Csharp :: how to remove white spaces from string in c# 
Csharp :: c# get foreground window 
Csharp :: round decimal two places 
Csharp :: c# delegate 
Csharp :: change column name in datatable C# 
Csharp :: c# read csv file 
Csharp :: ffmpeg add audio to video at specific time 
Csharp :: c# function return 
Csharp :: c# array.join 
Csharp :: sqldatareader in c# 
Csharp :: set rotation unity 
Csharp :: multiplication using arrays 
Csharp :: c# use api rest 
Csharp :: asp.net mvc get current url in view 
Csharp :: detect collision in unity 
Csharp :: datetime month name 
Csharp :: linq query get last day of month 
Csharp :: save image in c# 
Csharp :: access object property C# 
Csharp :: dotween sequence 
Csharp :: unity onclick object 
Csharp :: replace first occurrence of character in string c# 
Csharp :: Winform on exit run method 
Csharp :: c# online compiler 
Csharp :: unity gameobject find inactive 
Csharp :: c# modify dictionary in loop 
Csharp :: element click intercepted exception in selenium C# 
Csharp :: c# how to compare 2 dates without time 
Csharp :: declare enum c# 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =