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. 在CentOS 7中安装nginx服务器

    简要地介绍一下,如何在CentOS 7中安装nginx服务器  下载对应当前系统版本的nginx包(package) # wget  http://nginx.org/packages/centos/ ...

  2. Swing列表框组件

    public class JListTest extends JFrame{    private static final long serialVersionUID=1L;        publ ...

  3. 58. N-Queens && N-Queens II

    N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...

  4. WWDC2014之iOS使用动态库 framework【转】

    from:http://www.cocoachina.com/industry/20140613/8810.html JUN 12TH, 2014 苹果的开放态度 WWDC2014上发布的Xcode6 ...

  5. 重装系统分区时,发现一个叫LVM的东西,找出来和大家分享

    LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前 ...

  6. WinServer2008R2 部署.NET4.0程序 注意事项

    部署注意事项:   1.IIS应用程序池 集成模式   2.在web.config中的system.webServer下,添加 <modules runAllManagedModulesForA ...

  7. Oracle命令:授权-收回权限-角色

    Oracle命令:授权-收回权限-角色 oracle grant 不论授予何种权限,每条授权(grant)语句总是由三部分组成: 1) 接受者部分是准备获得权限的一个或多个用户的列表. 2)关键字权限 ...

  8. Using AFNetWorking 2.0 upload file to php web service server based on Slim

    Recently i am developing the IOS app, a feature is needed to upload image to the webservice server. ...

  9. java中多线程中Runnable接口和Thread类介绍

    java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...

  10. tableview的顶部有一部分空白区域,并不是Cell的解决方法。

    self.automaticallyAdjustsScrollViewInsets = false: