人民医院API

DataTableExtensions.cs 6.4KB

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