DataRowExtensions.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using EasyDevCore.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace EasyDevCore.Database.EntityTable
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public static class DataRowExtensions
  15. {
  16. /// <summary>
  17. /// Gets a value indicating whether this instance is new.
  18. /// </summary>
  19. /// <value>
  20. /// <c>true</c> if this instance is new; otherwise, <c>false</c>.
  21. /// </value>
  22. public static bool IsNew(this DataRow row) => row.RowState == DataRowState.Added;
  23. /// <summary>
  24. /// Gets a value indicating whether this instance is deleted.
  25. /// </summary>
  26. /// <value>
  27. /// <c>true</c> if this instance is new; otherwise, <c>false</c>.
  28. /// </value>
  29. public static bool IsDeleted(this DataRow row) => row.RowState == DataRowState.Deleted;
  30. /// <summary>
  31. /// Gets a value indicating whether this instance is changed.
  32. /// </summary>
  33. /// <value>
  34. /// <c>true</c> if this instance is changed; otherwise, <c>false</c>.
  35. /// </value>
  36. public static bool IsChanged(this DataRow row) => row.RowState == DataRowState.Added || row.RowState == DataRowState.Deleted || row.RowState == DataRowState.Modified;
  37. /// <summary>
  38. /// Determines whether the specified column index is changed.
  39. /// </summary>
  40. /// <param name="row">The row.</param>
  41. /// <param name="columnIndex">Index of the column.</param>
  42. /// <returns>
  43. /// <c>true</c> if the specified column index is changed; otherwise, <c>false</c>.
  44. /// </returns>
  45. public static bool IsChanged(this DataRow row, int columnIndex)
  46. {
  47. var curValue = row.HasVersion(DataRowVersion.Current) ? row[columnIndex, DataRowVersion.Current] : null;
  48. var oldValue = row.HasVersion(DataRowVersion.Original) ? row[columnIndex, DataRowVersion.Original] : null;
  49. if (oldValue != null && curValue == null || oldValue == null && curValue != null
  50. || oldValue != curValue)
  51. {
  52. return true;
  53. }
  54. return false;
  55. }
  56. /// <summary>
  57. /// Determines whether the specified column name is changed.
  58. /// </summary>
  59. /// <param name="row">The row.</param>
  60. /// <param name="columnName">Name of the column.</param>
  61. /// <returns>
  62. /// <c>true</c> if the specified column name is changed; otherwise, <c>false</c>.
  63. /// </returns>
  64. public static bool IsChanged(this DataRow row, string columnName)
  65. {
  66. var curValue = row.HasVersion(DataRowVersion.Current) ? row[columnName, DataRowVersion.Current] : null;
  67. var oldValue = row.HasVersion(DataRowVersion.Original) ? row[columnName, DataRowVersion.Original] : null;
  68. if (oldValue != null && curValue == null || oldValue == null && curValue != null
  69. || oldValue != curValue)
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /// <summary>Gets the value, if value = DBNull.Value, return null value</summary>
  76. /// <param name="row"></param>
  77. /// <param name="columnIndex">Index of the column.</param>
  78. /// <returns>
  79. /// </returns>
  80. public static object GetValue(this DataRow row, int columnIndex) => row[columnIndex] == DBNull.Value ? null : row[columnIndex];
  81. /// <summary>
  82. /// Gets the value, if value = DBNull.Value, return null value
  83. /// </summary>
  84. /// <param name="row">The row.</param>
  85. /// <param name="columnName">Name of the column.</param>
  86. /// <returns></returns>
  87. public static object GetValue(this DataRow row, string columnName) => row[columnName] == DBNull.Value ? null : row[columnName];
  88. /// <summary>
  89. /// Gets the value, if value = DBNull.Value, return valueIfNull value
  90. /// </summary>
  91. /// <typeparam name="T"></typeparam>
  92. /// <param name="row">The row.</param>
  93. /// <param name="columnIndex">Index of the column.</param>
  94. /// <param name="valueIfNull">The value if null.</param>
  95. /// <returns></returns>
  96. public static T GetValue<T>(this DataRow row, int columnIndex, T valueIfNull = default) => row[columnIndex] == DBNull.Value ? valueIfNull : (T)row[columnIndex];
  97. /// <summary>
  98. /// Gets the value, if value = DBNull.Value, return valueIfNull value
  99. /// </summary>
  100. /// <typeparam name="T"></typeparam>
  101. /// <param name="row">The row.</param>
  102. /// <param name="columnName">Name of the column.</param>
  103. /// <param name="valueIfNull">The value if null.</param>
  104. /// <returns></returns>
  105. public static T GetValue<T>(this DataRow row, string columnName, T valueIfNull = default) => row[columnName] == DBNull.Value ? valueIfNull : (T)row[columnName];
  106. /// <summary>
  107. /// Sets the value.
  108. /// </summary>
  109. /// <param name="row">The row.</param>
  110. /// <param name="columnIndex">Index of the column.</param>
  111. /// <param name="value">The value.</param>
  112. /// <returns></returns>
  113. public static bool SetValue(this DataRow row, int columnIndex, object value)
  114. {
  115. object oldValue = row[columnIndex];
  116. bool newValueIsNull = value == null || value.Equals(DBNull.Value);
  117. bool oldValueIsNull = oldValue == DBNull.Value;
  118. if (!newValueIsNull || !oldValueIsNull)
  119. {
  120. if (newValueIsNull)
  121. {
  122. row[columnIndex] = DBNull.Value;
  123. return true;
  124. }
  125. if (oldValueIsNull || !value.Equals(oldValue))
  126. {
  127. row[columnIndex] = value;
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. /// <summary>
  134. /// Sets the value.
  135. /// </summary>
  136. /// <param name="row">The row.</param>
  137. /// <param name="columnName">Name of the column.</param>
  138. /// <param name="value">The value.</param>
  139. /// <returns></returns>
  140. public static bool SetValue(this DataRow row, string columnName, object value) => row.SetValue(row.Table.Columns[columnName].Ordinal, value);
  141. /// <summary>
  142. /// Deletes the specified source.
  143. /// </summary>
  144. /// <typeparam name="TSource">The type of the source.</typeparam>
  145. /// <param name="source">The source.</param>
  146. /// <returns></returns>
  147. public static int Delete<TSource>(this IEnumerable<TSource> source)
  148. where TSource : DataRow
  149. {
  150. int rowsEffected = 0;
  151. foreach (var row in source)
  152. {
  153. row.Delete();
  154. rowsEffected++;
  155. }
  156. return rowsEffected;
  157. }
  158. /// <summary>
  159. /// Updates the DataRow.
  160. /// </summary>
  161. /// <typeparam name="TSource">The type of the source.</typeparam>
  162. /// <param name="source">The source.</param>
  163. /// <param name="values">The values.</param>
  164. /// <returns></returns>
  165. /// <exception cref="Exception">string.Format("Invalid column name [{0}]", kp.Key)</exception>
  166. public static TSource Update<TSource>(this TSource source, params object[] values)
  167. where TSource : DataRow
  168. {
  169. var args = ArgumentHelper.GetArgValues(values);
  170. var table = source.Table;
  171. foreach (var kp in args)
  172. {
  173. int colIndex = table.Columns.IndexOf(kp.Key);
  174. if (colIndex >= 0)
  175. {
  176. source.SetValue(colIndex, kp.Value);
  177. }
  178. else
  179. {
  180. throw new Exception(string.Format("Invalid column name [{0}]", kp.Key));
  181. }
  182. }
  183. return source;
  184. }
  185. /// <summary>
  186. /// Updates this instance with the values of the data row
  187. /// </summary>
  188. /// <param name="row">The data row.</param>
  189. /// <param name="source">The source.</param>
  190. /// <param name="columnNames">The colum names filter (can be use = for mapping; ex: ColumnName = PropertyName).</param>
  191. /// <param name="exceptColumnNames">The except column names (splits by comma).</param>
  192. /// <returns></returns>
  193. public static DataRow UpdateFrom(this DataRow row, DataRow source, string columnNames = "", string exceptColumnNames = "")
  194. {
  195. IDictionary<string, string> listColumns = row.Table.GetMatchedColumnsList(source.Table, columnNames, exceptColumnNames);
  196. foreach (var map in listColumns)
  197. {
  198. row[map.Key] = source[map.Value];
  199. }
  200. return row;
  201. }
  202. /// <summary>
  203. /// Updates this row with the values of the row view
  204. /// </summary>
  205. /// <param name="row">The row.</param>
  206. /// <param name="source">The row view.</param>
  207. /// <param name="columnNames">The colum names filter (can be use = for mapping; ex: ColumnName = PropertyName).</param>
  208. /// <param name="exceptColumnNames">The except column names (splits by comma).</param>
  209. /// <returns></returns>
  210. public static DataRow UpdateFrom(this DataRow row, DataRowView source, string columnNames = "", string exceptColumnNames = "")
  211. {
  212. IDictionary<string, string> listColumns = row.Table.GetMatchedColumnsList(source.Row.Table, columnNames, exceptColumnNames);
  213. foreach (var map in listColumns)
  214. {
  215. row[map.Key] = source[map.Value];
  216. }
  217. return row;
  218. }
  219. /// <summary>
  220. /// Updates to model.
  221. /// </summary>
  222. /// <typeparam name="T"></typeparam>
  223. /// <param name="source">The source.</param>
  224. /// <param name="model">The model.</param>
  225. /// <param name="listProperties">The list properties.</param>
  226. /// <param name="modelStatusField">The model status field.</param>
  227. internal static void UpdateToModel<T>(this DataRow source, T model, IDictionary<string, PropertyDescriptor> listProperties, string modelStatusField = "")
  228. {
  229. string propName = string.Empty;
  230. try
  231. {
  232. foreach (var prop in listProperties)
  233. {
  234. PropertyDescriptor p = prop.Value;
  235. p.SetValue(model, source[prop.Key]);
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. ex.Data["PropertyName"] = propName;
  241. throw;
  242. }
  243. if (!string.IsNullOrWhiteSpace(modelStatusField)) model.SetPropertyValue(modelStatusField, source.RowState);
  244. }
  245. /// <summary>
  246. /// Converts to model.
  247. /// </summary>
  248. /// <typeparam name="T"></typeparam>
  249. /// <param name="source">The source.</param>
  250. /// <param name="listProperties">The list properties.</param>
  251. /// <param name="modelStatusField">The model status field has receive status of source (field type must be DataRowState).</param>
  252. /// <param name="method">The method.</param>
  253. /// <returns></returns>
  254. internal static T ConvertToModel<T>(this DataRow source, IDictionary<string, PropertyDescriptor> listProperties, string modelStatusField = "", Action<T> method = null)
  255. {
  256. T item = default;
  257. item = Activator.CreateInstance<T>();
  258. source.UpdateToModel(item, listProperties, modelStatusField);
  259. if (method != null) method(item);
  260. return item;
  261. }
  262. /// <summary>
  263. /// Convert to Model DataRow.
  264. /// </summary>
  265. /// <typeparam name="T"></typeparam>
  266. /// <param name="source">The source.</param>
  267. /// <param name="columnNames">The colum names filter (can be use = for mapping; ex: ColumnName = PropertyName).</param>
  268. /// <param name="exceptColumnNames">The except column names.</param>
  269. /// <param name="modelStatusField">The model status field has receive status of source (field type must be DataRowState).</param>
  270. /// <param name="ignorePrefix">The ignore prefix for ignore prefix characters when mapping field name with column name.</param>
  271. /// <param name="method">The method.</param>
  272. /// <returns>T.</returns>
  273. public static T ConvertToModel<T>(this DataRow source, string columnNames = "", string exceptColumnNames = "", string modelStatusField = "", string ignorePrefix = "", Action<T> method = null)
  274. {
  275. Type DataRowType = typeof(T);
  276. IDictionary<string, PropertyDescriptor> properties = (source.Table as DataTable).GetMatchedPropertiesList<T>(columnNames, exceptColumnNames, ignorePrefix);
  277. T item = source.ConvertToModel<T>(properties, modelStatusField);
  278. if (method != null) method(item);
  279. return item;
  280. }
  281. /// <summary>
  282. /// Updates from model.
  283. /// </summary>
  284. /// <typeparam name="T"></typeparam>
  285. /// <param name="source">The source.</param>
  286. /// <param name="item">The item.</param>
  287. /// <param name="listProperties">The list properties.</param>
  288. internal static void UpdateFromModel<T>(this DataRow source, T item, IDictionary<string, PropertyDescriptor> listProperties)
  289. {
  290. string propName = string.Empty;
  291. try
  292. {
  293. foreach (var prop in listProperties)
  294. {
  295. PropertyDescriptor p = prop.Value;
  296. propName = prop.Key;
  297. source[propName] = p.GetValue(item);
  298. }
  299. }
  300. catch (Exception ex)
  301. {
  302. ex.Data["PropertyName"] = propName;
  303. throw;
  304. }
  305. }
  306. /// <summary>
  307. /// Updates from Model DataRow.
  308. /// </summary>
  309. /// <typeparam name="T"></typeparam>
  310. /// <param name="source">The source.</param>
  311. /// <param name="item">The item.</param>
  312. /// <param name="columnNames">The colum names filter (can be use = for mapping; ex: ColumnName = PropertyName).</param>
  313. /// <param name="exceptColumnNames">The except column names.</param>
  314. public static void UpdateFromModel<T>(this DataRow source, T item, string columnNames = "", string exceptColumnNames = "")
  315. {
  316. IDictionary<string, PropertyDescriptor> properties = (source.Table as DataTable).GetMatchedPropertiesList<T>(columnNames, exceptColumnNames);
  317. source.UpdateFromModel(item, properties);
  318. }
  319. /// <summary>
  320. /// Updates to model.
  321. /// </summary>
  322. /// <typeparam name="T"></typeparam>
  323. /// <param name="source">The source.</param>
  324. /// <param name="model">The model.</param>
  325. /// <param name="columnNames">The colum names filter (can be use = for mapping; ex: ColumnName = PropertyName).</param>
  326. /// <param name="exceptColumnNames">The except column names.</param>
  327. /// <param name="modelStatusField">The model status field.</param>
  328. public static void UpdateToModel<T>(this DataRow source, T model, string columnNames = "", string exceptColumnNames = "", string modelStatusField = "")
  329. {
  330. IDictionary<string, PropertyDescriptor> properties = (source.Table as DataTable).GetMatchedPropertiesList<T>(columnNames, exceptColumnNames);
  331. source.UpdateToModel(model, properties, modelStatusField);
  332. }
  333. }
  334. }