public class List2DataTable     {

public static string GetClassName(Type type)

{             if (type == null)

throw new ArgumentException("参数type不能为空");

if (type.HasAttribute<AliasAttribute>())

return type.GetAttribute<AliasAttribute>().Name.ToLower();

else

return type.Name.ToLower();

}

public static DataTable ListToDataTable<T>(List<T> oList) where T : class

{

try

{

string tableName = GetClassName(typeof(T));

DataTable dt = new DataTable();

string fieldName;

object fieldValue;

string[] fieldNames;

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

fieldNames = new string[Props.Length];

for (int i = 0; i < Props.Length; i++)

{

fieldName = Props[i].Name;

fieldNames[i] = fieldName;

dt.Columns.Add(fieldName, Props[i].PropertyType);

}

for (int r = 0; r < oList.Count; r++)

{

DataRow dr = dt.NewRow();

T o = oList[r];

for (int i = 0; i < fieldNames.Length; i++)

{

fieldValue = Props[i].GetValue(o, null);

dr[fieldNames[i]] = fieldValue;

}

dt.Rows.Add(dr);

}

return dt;

}

catch(Exception ex)             {             }

return null;

}

}

List<T> 转换 DataTable的更多相关文章

  1. dataGrid转换dataTable

    #region dataGrid转换dataTable   /// <summary>   /// dataGrid转换dataTable   /// </summary>   ...

  2. C# 将Excel以文件流转换DataTable

    /* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...

  3. List转换DataTable

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  4. DataRow数组转换DataTable

    public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...

  5. C# DataRow[]转换DataTable

    DataTable dt = ... DataRow[] dr = dt.Select("ID=14"); dt = dr.CopyToDataTable();

  6. c# 将csv文件转换datatable的两种方式。

    第一种: public static DataTable csvdatatable(string path) { DataTable dt = new DataTable(); string conn ...

  7. 实体lis<T>t转换datatable

    public static DataTable ListToTable<T>(List<T> list) {             Type type = typeof(T) ...

  8. 读CSV转换datatable

    using System.Data; using System.IO;   /// <summary> /// Stream读取.csv文件 /// </summary> // ...

  9. SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表

    DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...

随机推荐

  1. angularjs实现时钟效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. 思维导图分享以及MindManager使用说明

    来源于: http://www.cnblogs.com/muhongxing/archive/2009/12/22/1628782.html http://www.cnblogs.com/muhong ...

  3. java之自定义回调接口

    本质上为:传递不同的实现的接口实例,执行不同的程序,即有扩展性. 在一个方法中,可以实现一个对象中的接口,实例化该接口,即可完成对不同对象的不同回掉. 在原有类中,调用接口中的方法,根据不同的接口实例 ...

  4. 【POJ 3261】Milk Patterns 可重叠的k次最长重复子串

    可重叠的k次最长重复子串 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  5. 【POJ 1228】Grandpa's Estate 凸包

    找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include ...

  6. oracle 知识

    sqlplus /  as sysdba;  使用操作系统登录oracle数据库 conn panie/panie;  使用普通用户连接数据库 --开启归档模式shutdown immediate;s ...

  7. 如何知道某个网站的IP地址

    命令行窗口得到ip地址 1按键盘上的win键(alt键左面的小窗口)+R键 调用运行 2输入cmd 确定 调用系统的 命令行窗口 3在命令行窗口里输入 ping + 空格键 + www.*****.c ...

  8. 简进祥===AFNetWorking 下载视频文件

    获取沙盒中的Documents地址的代码. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUs ...

  9. python 学习笔记12(序列常用方法总结)

    http://www.cnblogs.com/vamei/archive/2012/07/19/2599940.html 多回想!!! 1. 序列(list,tuple,string) len(s) ...

  10. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...