• DataTableResponseEntity


    1. ///
    2. /// DataTable数据集合返回实体
    3. ///
    4. [Serializable]
    5. public sealed class DataTableResponseEntity : IResponse
    6. {
    7. private readonly DataTable sourceTable;
    8. private readonly int PageCount;
    9. private readonly int totalCount;
    10. private readonly Dictionary<string, Tuplestring, Func<object, object>>> addValuePairs = new Dictionary<string, Tuplestring, Func<object, object>>>();
    11. private readonly Dictionary<string, string> removeValuePairs = new Dictionary<string, string>();
    12. private readonly Dictionary<string, Tuplestring, Func<object, object>>> editValuePairs = new Dictionary<string, Tuplestring, Func<object, object>>>();
    13. ///
    14. /// 构造函数
    15. ///
    16. public DataTableResponseEntity(DataTable dataTable)
    17. : this(dataTable, dataTable.Rows.Count)
    18. {
    19. }
    20. ///
    21. /// 构造函数
    22. ///
    23. public DataTableResponseEntity(DataTable dataTable, int totalCount)
    24. {
    25. this.sourceTable = dataTable;
    26. this.PageCount = dataTable.Rows.Count;
    27. this.totalCount = totalCount;
    28. }
    29. ///
    30. /// 获取总数
    31. ///
    32. public int TotalCount { get { return this.totalCount; } }
    33. ///
    34. /// 是否包含行
    35. ///
    36. public bool HasRows
    37. {
    38. get { return this.PageCount > 0; }
    39. }
    40. ///
    41. ///
    42. ///
    43. public int RowsCount { get { return this.sourceTable.Rows.Count; } }
    44. ///
    45. /// 获取字段的值
    46. ///
    47. ///
    48. public T SafeRead<T>(int rows, string fieldName)
    49. {
    50. if (rows < 0 || rows > this.PageCount)
    51. return default;
    52. var dataRow = this.sourceTable.Rows[rows];
    53. if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))
    54. return default;
    55. if (this.sourceTable.Columns[fieldName].DataType != typeof(T))
    56. return (T)Convert.ChangeType(dataRow[fieldName], typeof(T));
    57. return dataRow.Field(fieldName);
    58. }
    59. ///
    60. /// 获取字段的值
    61. ///
    62. ///
    63. public T SafeRead<T>(int rows, string fieldName, Func<object, T> funValue)
    64. {
    65. if (rows < 0 || rows > this.PageCount)
    66. return default;
    67. var dataRow = this.sourceTable.Rows[rows];
    68. if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))
    69. return funValue.Invoke(null);
    70. if (this.sourceTable.Columns[fieldName].DataType != typeof(T))
    71. return funValue.Invoke(dataRow[fieldName]);
    72. return funValue.Invoke(dataRow.Field(fieldName));
    73. }
    74. ///
    75. /// 获取字段的值
    76. ///
    77. ///
    78. public void SafeWrite<T>(int rows, string fieldName, T writeValue, bool isThrow = false)
    79. {
    80. if (rows < 0 || rows > this.PageCount)
    81. return;
    82. var dataRow = this.sourceTable.Rows[rows];
    83. if (this.sourceTable.Columns.Contains(fieldName) == false)
    84. {
    85. if (isThrow)
    86. return;
    87. this.sourceTable.Columns.Add(fieldName, typeof(T));
    88. dataRow.SetField(fieldName, writeValue);
    89. return;
    90. }
    91. if (this.sourceTable.Columns[fieldName].DataType != typeof(T))
    92. {
    93. object newValue = Convert.ChangeType(writeValue, this.sourceTable.Columns[fieldName].DataType);
    94. dataRow[fieldName] = newValue;
    95. return;
    96. }
    97. dataRow.SetField(fieldName, writeValue);
    98. }
    99. ///
    100. /// 获取字段的值
    101. ///
    102. ///
    103. public T SafeRead<T>(int rows, string fieldName, T defualtValue)
    104. {
    105. if (rows < 0 || rows > this.PageCount)
    106. return default;
    107. var dataRow = this.sourceTable.Rows[rows];
    108. if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))
    109. return defualtValue;
    110. if (this.sourceTable.Columns[fieldName].DataType != typeof(T))
    111. return defualtValue;
    112. return dataRow.Field(fieldName);
    113. }
    114. ///
    115. /// 获取字段的值
    116. ///
    117. ///
    118. public T SafeRead<T>(DataRow dataRow, string fieldName, Func<object, DataColumn, T> funValue)
    119. {
    120. if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))
    121. return funValue.Invoke(null, null);
    122. if (this.sourceTable.Columns[fieldName].DataType != typeof(T))
    123. return funValue.Invoke(dataRow[fieldName], this.sourceTable.Columns[fieldName]);
    124. return funValue.Invoke(dataRow.Field(fieldName), this.sourceTable.Columns[fieldName]);
    125. }
    126. ///
    127. ///
    128. ///
    129. ///
    130. ///
    131. public T DoWhile<T>(Func ExecuteTask)
    132. {
    133. return ExecuteTask.Invoke(this);
    134. }
    135. ///
    136. ///
    137. ///
    138. ///
    139. public EnumerableRowCollection AsEnumerable()
    140. {
    141. return this.sourceTable.AsEnumerable();
    142. }
    143. private bool ColumnEqual(object objectA, object objectB)
    144. {
    145. if (objectA == DBNull.Value && objectB == DBNull.Value)
    146. return true;
    147. if (objectA == DBNull.Value || objectB == DBNull.Value)
    148. return false;
    149. return (objectA.Equals(objectB));
    150. }
    151. private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns)
    152. {
    153. bool result = true;
    154. for (int i = 0; i < columns.Count; i++)
    155. result &= ColumnEqual(rowA[columns[i].ColumnName], rowB[columns[i].ColumnName]);
    156. return result;
    157. }
    158. ///
    159. /// 按照fieldName从sourceTable中选择出不重复的行,
    160. /// 相当于select distinct fieldName1,fieldName2,,fieldNamen from sourceTable
    161. ///
    162. ///
    163. /// 列名数组
    164. /// 一个新的不含重复行的DataTable,列只包括fieldNames中指明的列
    165. public DataTableResponseEntity Split(string filterExpression, string[] fieldNames = null)
    166. {
    167. DataTable resultTable = new DataTable();
    168. foreach (var field in fieldNames)
    169. {
    170. if (string.IsNullOrEmpty(field) || resultTable.Columns.Contains(field))
    171. continue;
    172. resultTable.Columns.Add(field, this.sourceTable.Columns[field].DataType);
    173. }
    174. object[] values = new object[fieldNames.Length];
    175. DataRow lastRow = null;
    176. foreach (DataRow dr in this.sourceTable.Select(filterExpression, string.Empty))
    177. {
    178. if (lastRow == null || !(RowEqual(lastRow, dr, resultTable.Columns)))
    179. {
    180. lastRow = dr;
    181. for (int i = 0; i < fieldNames.Length; i++)
    182. values[i] = dr[fieldNames[i]];
    183. resultTable.Rows.Add(values);
    184. }
    185. }
    186. return new DataTableResponseEntity(resultTable);
    187. }
    188. ///
    189. ///
    190. ///
    191. ///
    192. public DataTable AsTable()
    193. {
    194. RemoveTableColumn();
    195. AddTableColumn();
    196. EditTableColumn();
    197. return this.sourceTable;
    198. }
    199. ///
    200. ///
    201. ///
    202. ///
    203. public string AsTableJson()
    204. {
    205. return JsonConvert.SerializeObject(this.sourceTable);
    206. }
    207. ///
    208. /// 获取列名称
    209. ///
    210. public IEnumerable<string> Columns
    211. {
    212. get
    213. {
    214. foreach (DataColumn dc in this.sourceTable.Columns)
    215. yield return dc.ColumnName;
    216. }
    217. }
    218. ///
    219. /// 获取列
    220. ///
    221. public IEnumerable DataColumns
    222. {
    223. get
    224. {
    225. foreach (DataColumn dc in this.sourceTable.Columns)
    226. yield return dc;
    227. }
    228. }
    229. ///
    230. /// 新增列
    231. ///
    232. ///
    233. public DataTableResponseEntity AddColumn(string newColumnName, Type newColumnType, object newColumnValue)
    234. {
    235. return this.AddColumn(newColumnName, newColumnType, string.Empty, (originalValue) => newColumnValue);
    236. }
    237. ///
    238. /// 新增列
    239. ///
    240. ///
    241. public DataTableResponseEntity AddColumn(string newColumnName, Type newColumnType, Func<object, object> valueFrom)
    242. {
    243. return this.AddColumn(newColumnName, newColumnType, string.Empty, valueFrom);
    244. }
    245. ///
    246. /// 新增列
    247. ///
    248. ///
    249. public DataTableResponseEntity AddColumn(bool isAdd, string newColumnName, Type newColumnType, Func<object, object> valueFrom)
    250. {
    251. if (isAdd == false)
    252. return this;
    253. return this.AddColumn(newColumnName, newColumnType, string.Empty, valueFrom);
    254. }
    255. ///
    256. /// 新增列
    257. ///
    258. ///
    259. public DataTableResponseEntity AddColumn(string newColumnName, Type newColumnType, string originalColumnName, Func<object, object> valueFrom)
    260. {
    261. this.addValuePairs.TryAdd(newColumnName, Tuple.Create(newColumnType, originalColumnName, valueFrom));
    262. return this;
    263. }
    264. ///
    265. /// 新增字段
    266. ///
    267. private void AddTableColumn()
    268. {
    269. if (this.sourceTable == null || this.sourceTable.Rows.Count <= 0 || addValuePairs == null || addValuePairs.Count <= 0)
    270. return;
    271. foreach (var newCol in addValuePairs)
    272. {
    273. if (this.sourceTable.Columns.Contains(newCol.Key))
    274. continue;
    275. if (newCol.Value.Item1 != null)
    276. this.sourceTable.Columns.Add(newCol.Key, newCol.Value.Item1);
    277. else
    278. this.sourceTable.Columns.Add(newCol.Key);
    279. }
    280. foreach (DataRow currentRow in this.sourceTable.Rows)
    281. {
    282. foreach (var newCol in addValuePairs)
    283. {
    284. var newValue = newCol.Value.Item3?.Invoke(!string.IsNullOrEmpty(newCol.Value.Item2) ? currentRow[newCol.Value.Item2] : null);
    285. currentRow.SetField(newCol.Key, newValue);
    286. }
    287. }
    288. }
    289. ///
    290. /// 删除列
    291. ///
    292. ///
    293. public DataTableResponseEntity RemoveColumn(string newColumnName)
    294. {
    295. this.removeValuePairs.TryAdd(newColumnName, newColumnName);
    296. return this;
    297. }
    298. ///
    299. /// 删除字段
    300. ///
    301. private void RemoveTableColumn()
    302. {
    303. if (this.sourceTable == null || this.sourceTable.Rows.Count <= 0 || this.removeValuePairs == null || this.removeValuePairs.Count <= 0)
    304. return;
    305. foreach (var newCol in removeValuePairs)
    306. {
    307. if (this.sourceTable.Columns.Contains(newCol.Key))
    308. this.sourceTable.Columns.Remove(newCol.Key);
    309. }
    310. }
    311. ///
    312. /// 修改列
    313. ///
    314. ///
    315. public DataTableResponseEntity EditColumn(string newColumnName, Type newColumnType, string originalColumnName, Func<object, object> valueFrom = null)
    316. {
    317. this.editValuePairs.TryAdd(newColumnName, Tuple.Create(newColumnType, originalColumnName, valueFrom));
    318. return this;
    319. }
    320. ///
    321. /// 修改字段
    322. ///
    323. private void EditTableColumn()
    324. {
    325. if (this.sourceTable == null || this.sourceTable.Rows.Count <= 0 || this.editValuePairs == null || this.editValuePairs.Count <= 0)
    326. return;
    327. //新增需要的 Where(t => this.currentTable.Columns[t.Value.Item2].DataType != t.Value.Item1)
    328. Dictionary<string, Tuple<string, Type>> mayAddCols = editValuePairs.ToDictionarystring, Tuplestring, Func<object, object>>>, string, Tuple<string, Type>>(keySelector: (keyPairItem) => keyPairItem.Key, elementSelector: (keyPairItem) => Tuple.Create(keyPairItem.Value.Item2, keyPairItem.Value.Item1));
    329. //editValuePairs KEY:新字段,Value:新类型,旧字段,值转换Func
    330. //mayAddCols KEY:新字段,Value:旧字段,新类型
    331. foreach (var ss in mayAddCols)
    332. {
    333. if (!this.sourceTable.Columns.Contains(ss.Key))
    334. this.sourceTable.Columns.Add(ss.Key, ss.Value.Item2);
    335. foreach (DataRow currentRow in this.sourceTable.Rows)
    336. {
    337. var newValue = currentRow[ss.Value.Item1];
    338. var editValue = editValuePairs.FirstOrDefault(y => y.Key == ss.Key);
    339. if (!string.IsNullOrEmpty(editValue.Key) && editValue.Value.Item3 != null)
    340. newValue = editValue.Value.Item3.Invoke(newValue);
    341. //设置到新列上
    342. currentRow.SetField(ss.Key, newValue);
    343. }
    344. }
    345. //避免多个新字段 取到同个旧字段的值
    346. foreach (var ss in mayAddCols)
    347. {
    348. if (this.sourceTable.Columns.Contains(ss.Value.Item1) && !ss.Value.Item1.Equals(ss.Key, StringComparison.OrdinalIgnoreCase))
    349. this.sourceTable.Columns.Remove(ss.Value.Item1);
    350. }
    351. }
    352. }

    使用:

    1. var currTable = dataTableResponseEntity
    2. .EditColumn("RowNo", typeof(Int64), "FSEQ", t => t)
    3. .EditColumn("MaterialCode", typeof(string), "FItemNumber", t => t.ToString())
    4. .EditColumn("Qty", typeof(decimal), "FQTY", t => t)
    5. .EditColumn("ErpId", typeof(string), "FID", t => t.ToString())
    6. .EditColumn("ErpSubId", typeof(string), "FEntryID", t => t.ToString());

  • 相关阅读:
    2024北京智慧养老展,北京养老应用软件展,北京陪护机器人展
    USB3.0:VL817Q7-C0的LAYOUT指南(三)
    DockerFile微服务实战
    多线程编程——基础语法篇
    43 干货系列从零用Rust编写负载均衡及代理,内网穿透方案完整部署
    40-SpringBoot
    Docker桥接网络分析
    深度神经网络预测模型,神经网络预测未来数据
    混动汽车消费者分析洞察?普通消费者如何看待混动汽车?
    u盘损坏如何恢复数据?u盘恢复数据,超好用的几个方法!
  • 原文地址:https://blog.csdn.net/fuweiping/article/details/132715881