Convert IEnumerable To DataTable
//The MIT License (MIT)
//http://arolibraries.codeplex.com/license
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
namespace AroLibraries.ExtensionMethods.Enumerable
{
public static class IEnumerableExt
{
public static DataTable Ext_ToDataTable<T>(this IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
FieldInfo[] oField = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type) rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof (Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
oField = ((Type) rec.GetType()).GetFields();
foreach (FieldInfo fieldInfo in oField)
{
Type colType = fieldInfo.FieldType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof (Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(fieldInfo.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
if (oProps != null)
{
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) ?? DBNull.Value;
}
}
if (oField != null)
{
foreach (FieldInfo fieldInfo in oField)
{
dr[fieldInfo.Name] = fieldInfo.GetValue(rec) ?? DBNull.Value;
}
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
}
}
Related examples in the same category