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和table的转换的更多相关文章

  1. 如何将jsp页面的table报表转换到excel报表导出

    假设这就是你的jsp页面: 我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中.那么这个页面会变成这个样子 在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章, ...

  2. sp 数据拼接html table表转换xml,发邮件

    USE [BES_ADV] GO /****** Object: StoredProcedure [dbo].[RSP_FN_UNAPPLIED_Mail_Reminder] Script Date: ...

  3. Bootstrap Table的例子(转载)

    转载自:http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#classes-table 使用的API: data1.json da ...

  4. Oracle数据库中日期/数字和字符之间的转换和计算

    --查出当前系统时间 select SYSDATE from table; --格式转换 -- TO_CHAR 把日期或数字转换为字符串 -- TO_CHAR(number, '格式') -- TO_ ...

  5. 【翻译】Flink Table Api & SQL —Streaming 概念 ——时间属性

    本文翻译自官网: Time Attributes   https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/str ...

  6. python运算符

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAIAAADzel4SAAAgAElEQVR4Aey9+bMcSXLnV1dmna/ejR

  7. 利用Servlet导出Excel

    -----因为Excel可以打开HTML文件,因此可以利用页面的Form表单把页面中的table内容提交给Servlet,然后后台把提交上来的table内容转换成文件流的形式,并以下载的形式转给客户端 ...

  8. 数据库多表连接方式介绍-HASH-JOIN

    1.概述 hash join是一种数据库在进行多表连接时的处理算法,对于多表连接还有两种比较常用的方式:sort merge-join 和 nested loop. 为了比较清楚的介绍hash joi ...

  9. 在SQL中使用CLR提供基本函数对二进制数据进行解析与构造

      二进制数据包的解析一般是借助C#等语言,在通讯程序中解析后形成字段,再统一单笔或者批量(表类型参数)提交至数据库,在通讯程序中,存在BINARY到struct再到table的转换. 现借助CLR提 ...

随机推荐

  1. .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  2. 【转】linux sed命令详解

    原文网址:http://www.iteye.com/topic/587673 1. Sed简介sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”( ...

  3. 【翻译】HTML5开发——轻量级Web Database存储库html5sql.js

    方式1: html5sql官方网址:http://html5sql.com/ 阅读之前,先看W3C关于WEB Database的一段话: Beware. This specification is n ...

  4. unity的prefab(预设)例子

    prefab用于预先设置一些控件,在需要的时候直接引用,简化开发,当然,你完全可以用写代码解决 在场景内新建一个空物体,绑定一个脚本 void Start () { GameObject cube = ...

  5. emacs之切换h/cpp配置

    emacsConfig/switch-file-setting.el (defun switch-c () (global-set-key (kbd "<C-return>&qu ...

  6. vim自定义配置之自动括号

    BundlenInstall安装auto-pairs vimConfig/plugin/auto-pairs-setting.vim let g:autopairsflymode=

  7. HDU 4586 Play the Dice(数学期望)

    Play the Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  8. Ubuntu下VIM使用指南

    基本命令: Esc:VIM中的万能功能键之一,基本上任何时候按这个键,都可以返回VIM的普通状态. i:在普通状态下按i可以进入“插入”编辑状态,这个时候按方向键移动光标,在想要输入的地方输入字符,用 ...

  9. tornado相关文章

    http://www.linuxzen.com/shi-yong-tornadojin-xing-wang-luo-yi-bu-bian-cheng.html http://cloudaice.com ...

  10. JavaScript Promise的学习笔记

    首先声明:本人今天刚接触Promise,通过一个例子,希望能更好的来理解,如果有不对的地方,还望指正 Promise是专门为解决 js中回调而引起的各种问题,而产生的. 在异步编程中,我们经常使用回调 ...