1. List<info> infos = Dal.GetInfos();
  2. DataTable dt = new DataTable();
  3. dt.Columns.Add("cName");
  4. foreach (var info in infos)
  5. {
  6. DataRow dr = dt.NewRow();
  7. dr["cName"] = info.Name;
  8. dt.Add(dr);
  9. }

网上的:

    1. public static class DataTableExtensions
    2. {
    3. /// <summary>
    4. /// 转化一个DataTable
    5. /// </summary>
    6. /// <typeparam name="T"></typeparam>
    7. /// <param name="list"></param>
    8. /// <returns></returns>
    9. public static DataTable ToDataTable<T>(this IEnumerable<T> list)
    10. {
    11. //创建属性的集合
    12. List<PropertyInfo> pList = new List<PropertyInfo>();
    13. //获得反射的入口
    14. Type type = typeof(T);
    15. DataTable dt = new DataTable();
    16. //把所有的public属性加入到集合 并添加DataTable的列
    17. Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
    18. foreach (var item in list)
    19. {
    20. //创建一个DataRow实例
    21. DataRow row = dt.NewRow();
    22. //给row 赋值
    23. pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
    24. //加入到DataTable
    25. dt.Rows.Add(row);
    26. }
    27. return dt;
    28. }
    29. /// <summary>
    30. /// DataTable 转换为List 集合
    31. /// </summary>
    32. /// <typeparam name="TResult">类型</typeparam>
    33. /// <param name="dt">DataTable</param>
    34. /// <returns></returns>
    35. public static List<T> ToList<T>(this DataTable dt) where T : class, new()
    36. {
    37. //创建一个属性的列表
    38. List<PropertyInfo> prlist = new List<PropertyInfo>();
    39. //获取TResult的类型实例  反射的入口
    40. Type t = typeof(T);
    41. //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
    42. Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
    43. //创建返回的集合
    44. List<T> oblist = new List<T>();
    45. foreach (DataRow row in dt.Rows)
    46. {
    47. //创建TResult的实例
    48. T ob = new T();
    49. //找到对应的数据  并赋值
    50. prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
    51. //放入到返回的集合中.
    52. oblist.Add(ob);
    53. }
    54. return oblist;
    55. }
    56. /// <summary>
    57. /// 将集合类转换成DataTable
    58. /// </summary>
    59. /// <param name="list">集合</param>
    60. /// <returns></returns>
    61. public static DataTable ToDataTableTow(IList list)
    62. {
    63. DataTable result = new DataTable();
    64. if (list.Count > 0)
    65. {
    66. PropertyInfo[] propertys = list[0].GetType().GetProperties();
    67. foreach (PropertyInfo pi in propertys)
    68. {
    69. result.Columns.Add(pi.Name, pi.PropertyType);
    70. }
    71. for (int i = 0; i < list.Count; i++)
    72. {
    73. ArrayList tempList = new ArrayList();
    74. foreach (PropertyInfo pi in propertys)
    75. {
    76. object obj = pi.GetValue(list[i], null);
    77. tempList.Add(obj);
    78. }
    79. object[] array = tempList.ToArray();
    80. result.LoadDataRow(array, true);
    81. }
    82. }
    83. return result;
    84. }
    85. /**/
    86. /// <summary>
    87. /// 将泛型集合类转换成DataTable
    88. /// </summary>
    89. /// <typeparam name="T">集合项类型</typeparam>
    90. /// <param name="list">集合</param>
    91. /// <returns>数据集(表)</returns>
    92. public static DataTable ToDataTable<T>(IList<T> list)
    93. {
    94. return ToDataTable<T>(list, null);
    95. }
    96. /**/
    97. /// <summary>
    98. /// 将泛型集合类转换成DataTable
    99. /// </summary>
    100. /// <typeparam name="T">集合项类型</typeparam>
    101. /// <param name="list">集合</param>
    102. /// <param name="propertyName">需要返回的列的列名</param>
    103. /// <returns>数据集(表)</returns>
    104. public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
    105. {
    106. List<string> propertyNameList = new List<string>();
    107. if (propertyName != null)
    108. propertyNameList.AddRange(propertyName);
    109. DataTable result = new DataTable();
    110. if (list.Count > 0)
    111. {
    112. PropertyInfo[] propertys = list[0].GetType().GetProperties();
    113. foreach (PropertyInfo pi in propertys)
    114. {
    115. if (propertyNameList.Count == 0)
    116. {
    117. result.Columns.Add(pi.Name, pi.PropertyType);
    118. }
    119. else
    120. {
    121. if (propertyNameList.Contains(pi.Name))
    122. result.Columns.Add(pi.Name, pi.PropertyType);
    123. }
    124. }
    125. for (int i = 0; i < list.Count; i++)
    126. {
    127. ArrayList tempList = new ArrayList();
    128. foreach (PropertyInfo pi in propertys)
    129. {
    130. if (propertyNameList.Count == 0)
    131. {
    132. object obj = pi.GetValue(list[i], null);
    133. tempList.Add(obj);
    134. }
    135. else
    136. {
    137. if (propertyNameList.Contains(pi.Name))
    138. {
    139. object obj = pi.GetValue(list[i], null);
    140. tempList.Add(obj);
    141. }
    142. }
    143. }
    144. object[] array = tempList.ToArray();
    145. result.LoadDataRow(array, true);
    146. }
    147. }
    148. return result;
    149. }
    150. }

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

  1. 对象列表转换为DataTable或DataTable转换为对象列表.

    /**********************************************************************************/ // 说明: 数据转换工具. ...

  2. linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)

    在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...

  3. Json 字符串 转换为 DataTable数据集合

    /// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...

  4. c#常用的Datable转换为json,以及json转换为DataTable操作方法

    #region  DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  5. [工具类]泛型集合转换为DataTable

    写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...

  6. 泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  7. 【c#操作office】--OleDbDataAdapter 与OleDbDataReader方式读取excel,并转换为datatable

    OleDbDataAdapter方式: /// <summary> /// 读取excel的表格放到DataTable中 ---OleDbDataAdapter /// </summ ...

  8. C#基础知识之泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  9. Enumerable转换为DataTable

    今天在项目组公共类库中发现一个 Enumerable类型转换为DataTable,写的挺精简的,拿出来跟大家共享一下. using System; using System.Collections.G ...

  10. 把List<T>转换为DataTable

    下面这个学习,把List<T>转换为Datatable. 下面先创建一个对象T: class Ay { private int _ID; public int ID { get { ret ...

随机推荐

  1. 静态库 && 动态库

    http://weihe6666.iteye.com/blog/1100065 http://www.cnblogs.com/skynet/p/3372855.html 静态库: 在链接阶段,将汇编生 ...

  2. struts2视频学习笔记 18(自定义拦截器)

    课时18 自定义拦截 因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defa ...

  3. c# xml的增删改查操作 xmlDocument 的用法

    1.将xml转换为DataTable string path = "";//xml的位置StringReader sr = null;XmlTextReader xmlReader ...

  4. bat产生随机数并复制文件及生成文件列表

    有这样一个场景:我需要将同一个文件复制为上千个文件,并且文件名应为随机数.为了简单起见,不想写程序,直接写个BAT来,方便,简单,易用: 1. 搞定用BAT产生32位随机数,存为变量并使用,保存以下代 ...

  5. POJ 1422 二分图(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7278   Accepted: 4318 Descript ...

  6. WDCP管理面板安装启动EXIF、bcmath完整步骤

    一般我们网站建设的需要,如果使用WDCP面板默认的功能就足够使用,如果需要特殊程序的特定组件支持,就需要独立的安装支持组件.比如一位朋友的程序需要支持EXIF.bcmath组件,这不老蒋寻找解决方法, ...

  7. 内工大acm校赛--整理代码

    题目:小明搜到一行无缩进无换行代码,请帮小明整理代码.无for语句和case语句,而且只有一个主函数.你只要控制注意“:”“{”“}”这三个符号带来的缩进和换行效果就行. Input: 输入只有一行, ...

  8. [vijos P1512] SuperBrother打鼹鼠

    这周好好码树状数组和线段树!!之前没写过二维树状数组,凭借一维的思路居然写了个比较像模像样的东西出来,原来我没那么脑残.唯一要注意的就是getsum四个矩形加减的边界条件,这里看了别人标程才意识到错误 ...

  9. 使用siege进行Web压力测试

    因为最近公司线上node项目出一些不稳定的情况,考虑在这方面能不能做进一步的优化,既然要做优化首先要知道问题出在哪里? 因为暂无法定位是node层问题还是api层出现问题,由于在开发环境小并发量程序运 ...

  10. Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示

    如下为本人亲证代码: 一: 编译器的安装与配置(环境不同,显示结果不同) 要使用MATLAB编译器,用户计算机上应用事先安装与MATLAB适配的以下任何一种ANSI C/C++编译器: 5.0.6.0 ...