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. 1031MVCC和事务浅析

    转自 http://blog.csdn.net/sofia1217/article/details/50778906 关于MVCC浅析,有些难度http://xuebinbin212.blog.163 ...

  2. git 保存用户名和密码

    打开TortoiseGit控制面板 点击 Edit global .gitconfig文件 添加 [credential] helper = store OK了 你再登录一次之后密码就被记住了

  3. Shell命令_if

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #if if [ 条件判断式 ] ...

  4. ActiveMQ_点对点队列(二)

      一.本文章包含的内容 1.列举了ActiveMQ中通过Queue方式发送.消费队列的代码(普通文本.json/xml字符串.对象数据) 2.spring+activemq方式   二.配置信息 1 ...

  5. 【POJ 1279】Art Gallery

    http://poj.org/problem?id=1279 裸的半平面交的模板,按极角排序后维护一个双端队列,不要忘了最后要去除冗余,即最后一条边(或者更多的边)一定在双端队列里,但它不一定构成半平 ...

  6. css 导航,菜单对应页面切换效果实现方法

    实现原理: 每个菜单有多个li标签,每个li标签含一个id,li标签的id用来标记:点击效果 每个页面有一个id,这个id的作用是对应每个li标签的点击链接对应的页面,它的作用是用来标记:li标签的h ...

  7. spoj705 后缀数组求不同子串的个数

    http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  Given a stri ...

  8. java-byte[]图片在页面展示

    public void img(HttpServletRequest req, HttpServletResponse res) { //res.setHeader("Content-Typ ...

  9. hibernate-DetachedCriteria实现关联表条件复查

    表结果如下 CREATE TABLE `ent_lable` ( `idStr` ) NOT NULL, `pk_1` ) NOT NULL, `pk_2` ) NOT NULL, PRIMARY K ...

  10. java线程安全

    (一).java并发之原子性与可见性 原子性 原子是世界上的最小单位,具有不可分割性.比如 a=0:(a非long和double类型) 这个操作是不可分割的,那么我们说这个操作时原子操作.再比如:a+ ...