public static class List2DataTable
{
#region "Convert Generic List to DataTable"
/// <summary>
/// Convert a List{T} to a DataTable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
public static DataTable ConvertToDataTable<T>(this List<T> items)
{
var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
} foreach (T item in items)
{
var values = new object[props.Length]; for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}
return tb;
} /// <summary>
/// Determine of specified type is nullable
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
} /// <summary>
/// Return underlying type if type is Nullable otherwise return the type.
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}
#endregion
}

DataTable与List互换的更多相关文章

  1. 完整DataTable与IList互换(转)

    public class CollectionHelper { private CollectionHelper() { } public static DataTable ConvertTo< ...

  2. xml与datatable类型互换

    //已测 private DataTable ConvertXMLToDataSet(string xmlData) { StringReader stream = null; XmlTextRead ...

  3. DataTable.Compute()用法

    DataTable.Compute()用法 2010-04-07 11:28 一.DataTable.Compute()方法說明如下 作用:          计算用来传递筛选条件的当前行上的给定表达 ...

  4. 转:DataTable.Compute()用法

    转自:http://www.cnblogs.com/fanyf/archive/2012/05/11/2495919.html一.DataTable.Compute()方法說明如下 作用: 计算用来传 ...

  5. 关于PagedDataSource分页属性与DataSet和DataTable详解

    Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...

  6. C# DataTable.Compute()用法

    DataTable.Compute()用法 2010-04-07 11:28 一.DataTable.Compute()方法說明如下 作用:          计算用来传递筛选条件的当前行上的给定表达 ...

  7. DataTable.Compute()

    一.DataTable.Compute()方法說明如下 作用:          计算用来传递筛选条件的当前行上的给定表达式. 格式為:          Object Compute (string ...

  8. C# DataSet与DataTable的区别和用法

    DataSet是数据集,DataTable是数据表,DataSet存储多个DataTable.DataSet和DataTable像是专门存储数据的一个容器,在你查询数据库得到一些结果时可以存在里面. ...

  9. C# DataSet与DataTable的区别和用法 ---转载

    C# DataSet与DataTable的区别和用法 转载:https://www.cnblogs.com/liuyi-li/p/6340411.html DataSet是数据集,DataTable是 ...

随机推荐

  1. php数据库访问

    从$res获取行数据的时候,处理mysql_fetch_row($res),还有三个方法,分别是 mysql_fetch_row($res); 返回一个所以的数组,速度较快. mysql_fetch_ ...

  2. 关于C++中的类型转换

    C++中定义了四种类型转换操作符:static_cast.const_cast.dynamic_cast和reinterpret_cast. static_cast的用法类似于C语言中的强制类型转换, ...

  3. impala简单使用

    impala-shell connect ha1:21000 更新元信息 invalidate metadata;

  4. Ubuntu-tomcat7目录

    ubuntu下通过apt-get install tomcat7方式安装的tomcat目录分布如下,做个记录: user@myserver:/var/lib/tomcat7$ ls -lttotal ...

  5. hive和ORACLE语法对比

  6. 开源文档管理工具Joomla的网站安装

    1.配置PHP开发环境(Apache.PHP.MySQL) 2.安装Joomla网站: 1. 下载安装包     http://www.joomla.org/download.html 2. 登陆Jo ...

  7. dll 导出函数名的那些事

    dll 导出函数名的那些事 关键字: VC++  DLL  导出函数 经常使用VC6的Dependency或者是Depends工具查看DLL导出函数的名字,会发现有DLL导出函数的名字有时大不相同,导 ...

  8. Java2OP

    Java2OP D:\Program Files (x86)\Embarcadero\Studio\18.0\bin\converters\java2op\Java2OP.exe Java2OP.ex ...

  9. Linux搭建smtp服务器+laravel5.2发邮件配置

    /** * 这里主要是想通过自己搭建smtp服务器,配置laravel5.2框架,实现邮箱发邮件功能, * 主要内容是搭建smtp服务器,laravel5.2发邮件顺手提一下 */ /** * 1.l ...

  10. hibernate学习(设计一对多 关系 映射)

    1,配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-conf ...