boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

C# – Convert Generic List to DataTable Example

Updated on     Kisan Patel

The following are the static functions in which if we pass a any type of Generic List as data source and it will convert it to DataTable type.

public static class Utils
{

      public static DataTable ToDataTable<TSource>(this IList<TSource> data)
      {
             DataTable dataTable = new DataTable(typeof(TSource).Name);
             PropertyInfo[] props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
             foreach (PropertyInfo prop in props)
             {
                dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
             }

             foreach (TSource item in data)
             {
                 var values = new object[props.Length];
                 for (int i = 0; i < props.Length; i++)
                 {
                      values[i] = props[i].GetValue(item, null);
                 }
                 dataTable.Rows.Add(values);
             }
             return dataTable;
       } 
}

Now, use above static method to convert generic list to datatable as mentioned in below:

//Query data and store into variable...
List users = context.Users.ToList();

//Convert it to DataTable
DataTable dt = dt.ToDataTable<User>();

C#

Leave a Reply