DataTableHelper.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace Masuit.Tools.Database
  8. {
  9. /// <summary>
  10. /// DataTable帮助类
  11. /// </summary>
  12. public static class DataTableHelper
  13. {
  14. /// <summary>
  15. /// 给DataTable增加一个自增列
  16. /// 如果DataTable 存在 identityid 字段 则 直接返回DataTable 不做任何处理
  17. /// </summary>
  18. /// <param name="dt">DataTable</param>
  19. /// <returns>返回Datatable 增加字段 identityid </returns>
  20. public static DataTable AddIdentityColumn(this DataTable dt)
  21. {
  22. if (!dt.Columns.Contains("identityid"))
  23. {
  24. dt.Columns.Add("identityid");
  25. for (int i = 0; i < dt.Rows.Count; i++)
  26. {
  27. dt.Rows[i]["identityid"] = (i + 1).ToString();
  28. }
  29. }
  30. return dt;
  31. }
  32. /// <summary>
  33. /// 检查DataTable 是否有数据行
  34. /// </summary>
  35. /// <param name="dt">DataTable</param>
  36. /// <returns>是否有数据行</returns>
  37. public static bool HasRows(this DataTable dt)
  38. {
  39. return dt.Rows.Count > 0;
  40. }
  41. /// <summary>
  42. /// datatable转List
  43. /// </summary>
  44. /// <typeparam name="T"></typeparam>
  45. /// <param name="dt"></param>
  46. /// <returns></returns>
  47. public static List<T> ToList<T>(this DataTable dt) where T : class, new()
  48. {
  49. var list = new List<T>();
  50. if (dt == null || dt.Rows.Count == 0)
  51. {
  52. return list;
  53. }
  54. list.AddRange(dt.Rows.Cast<DataRow>().Select(info => DataTableBuilder<T>.CreateBuilder(dt.Rows[0]).Build(info)));
  55. return list;
  56. }
  57. /// <summary>
  58. /// 将泛型集合类转换成DataTable
  59. /// </summary>
  60. /// <typeparam name="T">集合项类型</typeparam>
  61. /// <param name="list">集合</param>
  62. /// <param name="tableName">表名</param>
  63. /// <returns>数据集(表)</returns>
  64. public static DataTable ToDataTable<T>(this IEnumerable<T> list, string tableName = null)
  65. {
  66. return ToDataTable(list.ToList(), tableName);
  67. }
  68. /// <summary>
  69. /// 将泛型集合类转换成DataTable
  70. /// </summary>
  71. /// <typeparam name="T">集合项类型</typeparam>
  72. /// <param name="list">集合</param>
  73. /// <param name="tableName">表名</param>
  74. /// <returns>数据集(表)</returns>
  75. public static DataTable ToDataTable<T>(this IList<T> list, string tableName = null)
  76. {
  77. var result = new DataTable(tableName);
  78. if (list.Count == 0)
  79. {
  80. foreach (var property in typeof(T).GetProperties())
  81. {
  82. var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
  83. if (underlyingType != null)
  84. {
  85. // 如果该类型具有可为空性
  86. var column = new DataColumn(property.Name, underlyingType);
  87. column.AllowDBNull = true;
  88. // 添加表头列,列名为属性名
  89. result.Columns.Add(column);
  90. }
  91. else
  92. {
  93. // 没有可为空性
  94. var column = new DataColumn(property.Name, property.PropertyType);
  95. // 添加表头列,列名为属性名
  96. result.Columns.Add(column);
  97. }
  98. }
  99. return result;
  100. }
  101. var properties = list[0].GetType().GetProperties();
  102. result.Columns.AddRange(properties.Select(p =>
  103. {
  104. if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
  105. {
  106. return new DataColumn(p.GetCustomAttribute<DescriptionAttribute>()?.Description ?? p.Name, Nullable.GetUnderlyingType(p.PropertyType));
  107. }
  108. return new DataColumn(p.GetCustomAttribute<DescriptionAttribute>()?.Description ?? p.Name, p.PropertyType);
  109. }).ToArray());
  110. list.ForEach(item => result.LoadDataRow(properties.Select(p => p.GetValue(item)).ToArray(), true));
  111. return result;
  112. }
  113. /// <summary>
  114. /// 根据nameList里面的字段创建一个表格,返回该表格的DataTable
  115. /// </summary>
  116. /// <param name="nameList">包含字段信息的列表</param>
  117. /// <returns>DataTable</returns>
  118. public static DataTable CreateTable(this List<string> nameList)
  119. {
  120. if (nameList.Count <= 0)
  121. {
  122. return null;
  123. }
  124. var myDataTable = new DataTable();
  125. foreach (string columnName in nameList)
  126. {
  127. myDataTable.Columns.Add(columnName, typeof(string));
  128. }
  129. return myDataTable;
  130. }
  131. /// <summary>
  132. /// 通过字符列表创建表字段,字段格式可以是:<br/>
  133. /// 1) a,b,c,d,e<br/>
  134. /// 2) a|int,b|string,c|bool,d|decimal<br/>
  135. /// </summary>
  136. /// <param name="dt"></param>
  137. /// <param name="nameString">字符列表</param>
  138. /// <returns>内存表</returns>
  139. public static DataTable CreateTable(this DataTable dt, string nameString)
  140. {
  141. string[] nameArray = nameString.Split(',', ';');
  142. foreach (string item in nameArray)
  143. {
  144. if (string.IsNullOrEmpty(item))
  145. {
  146. continue;
  147. }
  148. string[] subItems = item.Split('|');
  149. if (subItems.Length == 2)
  150. {
  151. dt.Columns.Add(subItems[0], ConvertType(subItems[1]));
  152. }
  153. else
  154. {
  155. dt.Columns.Add(subItems[0]);
  156. }
  157. }
  158. return dt;
  159. }
  160. /// <summary>
  161. /// 根据类型名返回一个Type类型
  162. /// </summary>
  163. /// <param name="typeName">类型的名称</param>
  164. /// <returns>Type对象</returns>
  165. private static Type ConvertType(string typeName) => typeName.ToLower().Replace("system.", "") switch
  166. {
  167. "boolean" => typeof(bool),
  168. "bool" => typeof(bool),
  169. "int16" => typeof(short),
  170. "short" => typeof(short),
  171. "int32" => typeof(int),
  172. "int" => typeof(int),
  173. "long" => typeof(long),
  174. "int64" => typeof(long),
  175. "uint16" => typeof(ushort),
  176. "ushort" => typeof(ushort),
  177. "uint32" => typeof(uint),
  178. "uint" => typeof(uint),
  179. "uint64" => typeof(ulong),
  180. "ulong" => typeof(ulong),
  181. "single" => typeof(float),
  182. "float" => typeof(float),
  183. "string" => typeof(string),
  184. "guid" => typeof(Guid),
  185. "decimal" => typeof(decimal),
  186. "double" => typeof(double),
  187. "datetime" => typeof(DateTime),
  188. "byte" => typeof(byte),
  189. "char" => typeof(char),
  190. _ => typeof(string)
  191. };
  192. /// <summary>
  193. /// 获得从DataRowCollection转换成的DataRow数组
  194. /// </summary>
  195. /// <param name="drc">DataRowCollection</param>
  196. /// <returns>DataRow数组</returns>
  197. public static DataRow[] GetDataRowArray(this DataRowCollection drc)
  198. {
  199. int count = drc.Count;
  200. var drs = new DataRow[count];
  201. for (int i = 0; i < count; i++)
  202. {
  203. drs[i] = drc[i];
  204. }
  205. return drs;
  206. }
  207. /// <summary>
  208. /// 将DataRow数组转换成DataTable,注意行数组的每个元素须具有相同的数据结构,
  209. /// 否则当有元素长度大于第一个元素时,抛出异常
  210. /// </summary>
  211. /// <param name="rows">行数组</param>
  212. /// <returns>将内存行组装成内存表</returns>
  213. public static DataTable GetTableFromRows(this DataRow[] rows)
  214. {
  215. if (rows.Length <= 0)
  216. {
  217. return new DataTable();
  218. }
  219. var dt = rows[0].Table.Clone();
  220. dt.DefaultView.Sort = rows[0].Table.DefaultView.Sort;
  221. foreach (var t in rows)
  222. {
  223. dt.LoadDataRow(t.ItemArray, true);
  224. }
  225. return dt;
  226. }
  227. }
  228. }