基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

156 line
5.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPA.SAAS.Manage.Comm.Util
  10. {
  11. public static class DataTableUitity
  12. {
  13. /// <summary>
  14. /// 将DataTable 转换成 List<dynamic>
  15. /// reverse 反转:控制返回结果中是只存在 FilterField 指定的字段,还是排除.
  16. /// [flase 返回FilterField 指定的字段]|[true 返回结果剔除 FilterField 指定的字段]
  17. /// FilterField 字段过滤,FilterField 为空 忽略 reverse 参数;返回DataTable中的全部数
  18. /// </summary>
  19. /// <param name="table">DataTable</param>
  20. /// <param name="reverse">
  21. /// 反转:控制返回结果中是只存在 FilterField 指定的字段,还是排除.
  22. /// [flase 返回FilterField 指定的字段]|[true 返回结果剔除 FilterField 指定的字段]
  23. ///</param>
  24. /// <param name="FilterField">字段过滤,FilterField 为空 忽略 reverse 参数;返回DataTable中的全部数据</param>
  25. /// <returns>List<dynamic></returns>
  26. public static List<dynamic> ToDynamicList(this DataTable table, bool reverse = true, params string[] FilterField)
  27. {
  28. var modelList = new List<dynamic>();
  29. foreach (DataRow row in table.Rows)
  30. {
  31. dynamic model = new ExpandoObject();
  32. var dict = (IDictionary<string, object>)model;
  33. foreach (DataColumn column in table.Columns)
  34. {
  35. if (FilterField.Length != 0)
  36. {
  37. if (reverse == true)
  38. {
  39. if (!FilterField.Contains(column.ColumnName))
  40. {
  41. dict[column.ColumnName] = row[column];
  42. }
  43. }
  44. else
  45. {
  46. if (FilterField.Contains(column.ColumnName))
  47. {
  48. dict[column.ColumnName] = row[column];
  49. }
  50. }
  51. }
  52. else
  53. {
  54. dict[column.ColumnName] = row[column];
  55. }
  56. }
  57. modelList.Add(model);
  58. }
  59. return modelList;
  60. }
  61. public static List<T> DatatTableToList<T>(DataTable dt) where T : class, new()
  62. {
  63. List<T> list = new List<T>();
  64. T t = new T();
  65. PropertyInfo[] prop = t.GetType().GetProperties();
  66. //遍历所有DataTable的行
  67. foreach (DataRow dr in dt.Rows)
  68. {
  69. t = new T();
  70. //通过反射获取T类型的所有成员
  71. foreach (PropertyInfo pi in prop)
  72. {
  73. //DataTable列名=属性名
  74. if (dt.Columns.Contains(pi.Name))
  75. {
  76. //属性值不为空
  77. if (dr[pi.Name] != DBNull.Value)
  78. {
  79. object value = Convert.ChangeType(dr[pi.Name], pi.PropertyType);
  80. //给T类型字段赋值
  81. pi.SetValue(t, value, null);
  82. }
  83. }
  84. }
  85. //将T类型添加到集合list
  86. list.Add(t);
  87. }
  88. return list;
  89. }
  90. public static T ConvertToEntity<T>(DataTable dt) where T : new()
  91. {
  92. //定义一个变量,用来接收属性名
  93. string tempType = string.Empty;
  94. T t = new T();
  95. //获得T的所有属性
  96. PropertyInfo[] propertyInfos = t.GetType().GetProperties();
  97. foreach (DataRow dr in dt.Rows)
  98. {
  99. foreach (PropertyInfo pi in propertyInfos)
  100. {
  101. //将属性名赋值给临时变量
  102. tempType = pi.Name;
  103. //如果表的列名不包含此属性,则跳过检查下一条属性
  104. if (!dt.Columns.Contains(tempType)) continue;
  105. //如果此属性可写
  106. if (pi.CanWrite)
  107. {
  108. object value = dr[tempType];
  109. //如果表中的字段值不为空,则赋值
  110. if (value != DBNull.Value)
  111. {
  112. value = Convert.ChangeType(value, pi.PropertyType);
  113. pi.SetValue(t, value, null);
  114. }
  115. }
  116. }
  117. }
  118. return t;
  119. }
  120. /// <summary>
  121. /// List转换为DataTable
  122. /// </summary>
  123. /// <typeparam name="T">List中的类型</typeparam>
  124. /// <param name="list">要转换的list</param>
  125. /// <returns></returns>
  126. public static DataTable ListToDataTable<T>(List<T> list) where T : class
  127. {
  128. DataTable dt = new DataTable();
  129. PropertyInfo[] prop = typeof(T).GetProperties();
  130. DataColumn[] ColumnArr = prop.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray();
  131. dt.Columns.AddRange(ColumnArr);
  132. foreach (T t in list)
  133. {
  134. DataRow dr = dt.NewRow();
  135. foreach (PropertyInfo pi in prop)
  136. {
  137. if (dt.Columns.Contains(pi.Name))
  138. {
  139. if (pi.GetValue(t) != null)
  140. {
  141. dr[pi.Name] = pi.GetValue(t);
  142. }
  143. }
  144. }
  145. dt.Rows.Add(dr);
  146. }
  147. return dt;
  148. }
  149. }
  150. }