/// <summary>/// DataTable数据集合返回实体/// </summary>[Serializable]public sealed class DataTableResponseEntity : IResponse{private readonly DataTable sourceTable;private readonly int PageCount;private readonly int totalCount;private readonly Dictionary<string, Tuple<Type, string, Func<object, object>>> addValuePairs = new Dictionary<string, Tuple<Type, string, Func<object, object>>>();private readonly Dictionary<string, string> removeValuePairs = new Dictionary<string, string>();private readonly Dictionary<string, Tuple<Type, string, Func<object, object>>> editValuePairs = new Dictionary<string, Tuple<Type, string, Func<object, object>>>();/// <summary>/// 构造函数/// </summary>public DataTableResponseEntity(DataTable dataTable): this(dataTable, dataTable.Rows.Count){}/// <summary>/// 构造函数/// </summary>public DataTableResponseEntity(DataTable dataTable, int totalCount){this.sourceTable = dataTable;this.PageCount = dataTable.Rows.Count;this.totalCount = totalCount;}/// <summary>/// 获取总数/// </summary>public int TotalCount { get { return this.totalCount; } }/// <summary>/// 是否包含行/// </summary>public bool HasRows{get { return this.PageCount > 0; }}/// <summary>/// /// </summary>public int RowsCount { get { return this.sourceTable.Rows.Count; } }/// <summary>/// 获取字段的值/// </summary>/// <returns></returns>public T SafeRead<T>(int rows, string fieldName){if (rows < 0 || rows > this.PageCount)return default;var dataRow = this.sourceTable.Rows[rows];if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))return default;if (this.sourceTable.Columns[fieldName].DataType != typeof(T))return (T)Convert.ChangeType(dataRow[fieldName], typeof(T));return dataRow.Field<T>(fieldName);}/// <summary>/// 获取字段的值/// </summary>/// <returns></returns>public T SafeRead<T>(int rows, string fieldName, Func<object, T> funValue){if (rows < 0 || rows > this.PageCount)return default;var dataRow = this.sourceTable.Rows[rows];if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))return funValue.Invoke(null);if (this.sourceTable.Columns[fieldName].DataType != typeof(T))return funValue.Invoke(dataRow[fieldName]);return funValue.Invoke(dataRow.Field<T>(fieldName));}/// <summary>/// 获取字段的值/// </summary>/// <returns></returns>public void SafeWrite<T>(int rows, string fieldName, T writeValue, bool isThrow = false){if (rows < 0 || rows > this.PageCount)return;var dataRow = this.sourceTable.Rows[rows];if (this.sourceTable.Columns.Contains(fieldName) == false){if (isThrow)return;this.sourceTable.Columns.Add(fieldName, typeof(T));dataRow.SetField<T>(fieldName, writeValue);return;}if (this.sourceTable.Columns[fieldName].DataType != typeof(T)){object newValue = Convert.ChangeType(writeValue, this.sourceTable.Columns[fieldName].DataType);dataRow[fieldName] = newValue;return;}dataRow.SetField<T>(fieldName, writeValue);}/// <summary>/// 获取字段的值/// </summary>/// <returns></returns>public T SafeRead<T>(int rows, string fieldName, T defualtValue){if (rows < 0 || rows > this.PageCount)return default;var dataRow = this.sourceTable.Rows[rows];if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))return defualtValue;if (this.sourceTable.Columns[fieldName].DataType != typeof(T))return defualtValue;return dataRow.Field<T>(fieldName);}/// <summary>/// 获取字段的值/// </summary>/// <returns></returns>public T SafeRead<T>(DataRow dataRow, string fieldName, Func<object, DataColumn, T> funValue){if (this.sourceTable.Columns.Contains(fieldName) == false || Convert.IsDBNull(dataRow[fieldName]))return funValue.Invoke(null, null);if (this.sourceTable.Columns[fieldName].DataType != typeof(T))return funValue.Invoke(dataRow[fieldName], this.sourceTable.Columns[fieldName]);return funValue.Invoke(dataRow.Field<T>(fieldName), this.sourceTable.Columns[fieldName]);}/// <summary>/// /// </summary>/// <typeparam name="T"></typeparam>/// <param name="ExecuteTask"></param>public T DoWhile<T>(Func<DataTableResponseEntity, T> ExecuteTask){return ExecuteTask.Invoke(this);}/// <summary>/// /// </summary>/// <returns></returns>public EnumerableRowCollection<DataRow> AsEnumerable(){return this.sourceTable.AsEnumerable();}private bool ColumnEqual(object objectA, object objectB){if (objectA == DBNull.Value && objectB == DBNull.Value)return true;if (objectA == DBNull.Value || objectB == DBNull.Value)return false;return (objectA.Equals(objectB));}private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns){bool result = true;for (int i = 0; i < columns.Count; i++)result &= ColumnEqual(rowA[columns[i].ColumnName], rowB[columns[i].ColumnName]);return result;}/// <summary> /// 按照fieldName从sourceTable中选择出不重复的行, /// 相当于select distinct fieldName1,fieldName2,,fieldNamen from sourceTable /// </summary> /// <param name="filterExpression"></param> /// <param name="fieldNames">列名数组</param> /// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns> public DataTableResponseEntity Split(string filterExpression, string[] fieldNames = null){DataTable resultTable = new DataTable();foreach (var field in fieldNames){if (string.IsNullOrEmpty(field) || resultTable.Columns.Contains(field))continue;resultTable.Columns.Add(field, this.sourceTable.Columns[field].DataType);}object[] values = new object[fieldNames.Length];DataRow lastRow = null;foreach (DataRow dr in this.sourceTable.Select(filterExpression, string.Empty)){if (lastRow == null || !(RowEqual(lastRow, dr, resultTable.Columns))){lastRow = dr;for (int i = 0; i < fieldNames.Length; i++)values[i] = dr[fieldNames[i]];resultTable.Rows.Add(values);}}return new DataTableResponseEntity(resultTable);}/// <summary>/// /// </summary>/// <returns></returns>public DataTable AsTable(){RemoveTableColumn();AddTableColumn();EditTableColumn();return this.sourceTable;}/// <summary>/// /// </summary>/// <returns></returns>public string AsTableJson(){return JsonConvert.SerializeObject(this.sourceTable);}/// <summary>/// 获取列名称/// </summary>public IEnumerable<string> Columns{get{foreach (DataColumn dc in this.sourceTable.Columns)yield return dc.ColumnName;}}/// <summary>/// 获取列/// </summary>public IEnumerable<DataColumn> DataColumns{get{foreach (DataColumn dc in this.sourceTable.Columns)yield return dc;}}/// <summary>/// 新增列/// </summary>/// <returns></returns>public DataTableResponseEntity AddColumn(string newColumnName, Type newColumnType, object newColumnValue){return this.AddColumn(newColumnName, newColumnType, string.Empty, (originalValue) => newColumnValue);}/// <summary>/// 新增列/// </summary>/// <returns></returns>public DataTableResponseEntity AddColumn(string newColumnName, Type newColumnType, Func<object, object> valueFrom){return this.AddColumn(newColumnName, newColumnType, string.Empty, valueFrom);}/// <summary>/// 新增列/// </summary>/// <returns></returns>public DataTableResponseEntity AddColumn(bool isAdd, string newColumnName, Type newColumnType, Func<object, object> valueFrom){if (isAdd == false)return this;return this.AddColumn(newColumnName, newColumnType, string.Empty, valueFrom);}/// <summary>/// 新增列/// </summary>/// <returns></returns>public DataTableResponseEntity AddColumn(string newColumnName, Type newColumnType, string originalColumnName, Func<object, object> valueFrom){this.addValuePairs.TryAdd(newColumnName, Tuple.Create(newColumnType, originalColumnName, valueFrom));return this;}/// <summary>/// 新增字段/// </summary>private void AddTableColumn(){if (this.sourceTable == null || this.sourceTable.Rows.Count <= 0 || addValuePairs == null || addValuePairs.Count <= 0)return;foreach (var newCol in addValuePairs){if (this.sourceTable.Columns.Contains(newCol.Key))continue;if (newCol.Value.Item1 != null)this.sourceTable.Columns.Add(newCol.Key, newCol.Value.Item1);elsethis.sourceTable.Columns.Add(newCol.Key);}foreach (DataRow currentRow in this.sourceTable.Rows){foreach (var newCol in addValuePairs){var newValue = newCol.Value.Item3?.Invoke(!string.IsNullOrEmpty(newCol.Value.Item2) ? currentRow[newCol.Value.Item2] : null);currentRow.SetField(newCol.Key, newValue);}}}/// <summary>/// 删除列/// </summary>/// <returns></returns>public DataTableResponseEntity RemoveColumn(string newColumnName){this.removeValuePairs.TryAdd(newColumnName, newColumnName);return this;}/// <summary>/// 删除字段/// </summary>private void RemoveTableColumn(){if (this.sourceTable == null || this.sourceTable.Rows.Count <= 0 || this.removeValuePairs == null || this.removeValuePairs.Count <= 0)return;foreach (var newCol in removeValuePairs){if (this.sourceTable.Columns.Contains(newCol.Key))this.sourceTable.Columns.Remove(newCol.Key);}}/// <summary>/// 修改列/// </summary>/// <returns></returns>public DataTableResponseEntity EditColumn(string newColumnName, Type newColumnType, string originalColumnName, Func<object, object> valueFrom = null){this.editValuePairs.TryAdd(newColumnName, Tuple.Create(newColumnType, originalColumnName, valueFrom));return this;}/// <summary>/// 修改字段/// </summary>private void EditTableColumn(){if (this.sourceTable == null || this.sourceTable.Rows.Count <= 0 || this.editValuePairs == null || this.editValuePairs.Count <= 0)return;//新增需要的 Where(t => this.currentTable.Columns[t.Value.Item2].DataType != t.Value.Item1)Dictionary<string, Tuple<string, Type>> mayAddCols = editValuePairs.ToDictionary<KeyValuePair<string, Tuple<Type, string, Func<object, object>>>, string, Tuple<string, Type>>(keySelector: (keyPairItem) => keyPairItem.Key, elementSelector: (keyPairItem) => Tuple.Create(keyPairItem.Value.Item2, keyPairItem.Value.Item1));//editValuePairs KEY:新字段,Value:新类型,旧字段,值转换Func//mayAddCols KEY:新字段,Value:旧字段,新类型foreach (var ss in mayAddCols){if (!this.sourceTable.Columns.Contains(ss.Key))this.sourceTable.Columns.Add(ss.Key, ss.Value.Item2);foreach (DataRow currentRow in this.sourceTable.Rows){var newValue = currentRow[ss.Value.Item1];var editValue = editValuePairs.FirstOrDefault(y => y.Key == ss.Key);if (!string.IsNullOrEmpty(editValue.Key) && editValue.Value.Item3 != null)newValue = editValue.Value.Item3.Invoke(newValue);//设置到新列上currentRow.SetField(ss.Key, newValue);}}//避免多个新字段 取到同个旧字段的值foreach (var ss in mayAddCols){if (this.sourceTable.Columns.Contains(ss.Value.Item1) && !ss.Value.Item1.Equals(ss.Key, StringComparison.OrdinalIgnoreCase))this.sourceTable.Columns.Remove(ss.Value.Item1);}}}
使用:
var currTable = dataTableResponseEntity.EditColumn("RowNo", typeof(Int64), "FSEQ", t => t).EditColumn("MaterialCode", typeof(string), "FItemNumber", t => t.ToString()).EditColumn("Qty", typeof(decimal), "FQTY", t => t).EditColumn("ErpId", typeof(string), "FID", t => t.ToString()).EditColumn("ErpSubId", typeof(string), "FEntryID", t => t.ToString());