using Common.Extensions; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace Common.Database { /// /// 数据库数据转换拓展 /// public static class DbDataConvertExtensions { /// /// 将 DataTable 转 List 集合 /// /// 返回值类型 /// DataTable /// List{T} public static List ToList(this DataTable dataTable) { return dataTable.ToList(typeof(List)) as List; } /// /// 将 DataTable 转 List 集合 /// /// 返回值类型 /// DataTable /// List{T} public static async Task> ToListAsync(this DataTable dataTable) { var list = await dataTable.ToListAsync(typeof(List)); return list as List; } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// DataSet /// 元组类型 public static List ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List)); return tuple[0] as List; } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List); } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2, List list3) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List, tuple[2] as List); } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2, List list3, List list4) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List), typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List, tuple[2] as List, tuple[3] as List); } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2, List list3, List list4, List list5) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List), typeof(List), typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List, tuple[2] as List, tuple[3] as List, tuple[4] as List); } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2, List list3, List list4, List list5, List list6) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List), typeof(List), typeof(List), typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List, tuple[2] as List, tuple[3] as List, tuple[4] as List, tuple[5] as List); } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2, List list3, List list4, List list5, List list6, List list7) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List), typeof(List), typeof(List), typeof(List), typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List, tuple[2] as List, tuple[3] as List, tuple[4] as List, tuple[5] as List, tuple[6] as List); } /// /// 将 DataSet 转 元组 /// /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// 元组元素类型 /// DataSet /// 元组类型 public static (List list1, List list2, List list3, List list4, List list5, List list6, List list7, List list8) ToList(this DataSet dataSet) { var tuple = dataSet.ToList(typeof(List), typeof(List), typeof(List), typeof(List), typeof(List), typeof(List), typeof(List), typeof(List)); return (tuple[0] as List, tuple[1] as List, tuple[2] as List, tuple[3] as List, tuple[4] as List, tuple[5] as List, tuple[6] as List, tuple[7] as List); } /// /// 将 DataSet 转 特定类型 /// /// DataSet /// 特定类型集合 /// List{object} public static List ToList(this DataSet dataSet, params Type[] returnTypes) { if (returnTypes == null || returnTypes.Length == 0) return default; // 处理元组类型 if (returnTypes.Length == 1 && returnTypes[0].IsValueType) { returnTypes = returnTypes[0].GenericTypeArguments; } // 获取所有的 DataTable var dataTables = dataSet.Tables; // 处理 8 个结果集 if (returnTypes.Length >= 8) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]), dataTables[2].ToList(returnTypes[2]), dataTables[3].ToList(returnTypes[3]), dataTables[4].ToList(returnTypes[4]), dataTables[5].ToList(returnTypes[5]), dataTables[6].ToList(returnTypes[6]), dataTables[7].ToList(returnTypes[7]) }; } // 处理 7 个结果集 else if (returnTypes.Length == 7) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]), dataTables[2].ToList(returnTypes[2]), dataTables[3].ToList(returnTypes[3]), dataTables[4].ToList(returnTypes[4]), dataTables[5].ToList(returnTypes[5]), dataTables[6].ToList(returnTypes[6]) }; } // 处理 6 个结果集 else if (returnTypes.Length == 6) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]), dataTables[2].ToList(returnTypes[2]), dataTables[3].ToList(returnTypes[3]), dataTables[4].ToList(returnTypes[4]), dataTables[5].ToList(returnTypes[5]) }; } // 处理 5 个结果集 else if (returnTypes.Length == 5) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]), dataTables[2].ToList(returnTypes[2]), dataTables[3].ToList(returnTypes[3]), dataTables[4].ToList(returnTypes[4]) }; } // 处理 4 个结果集 else if (returnTypes.Length == 4) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]), dataTables[2].ToList(returnTypes[2]), dataTables[3].ToList(returnTypes[3]) }; } // 处理 3 个结果集 else if (returnTypes.Length == 3) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]), dataTables[2].ToList(returnTypes[2]) }; } // 处理 2 个结果集 else if (returnTypes.Length == 2) { return new List { dataTables[0].ToList(returnTypes[0]), dataTables[1].ToList(returnTypes[1]) }; } // 处理 1 个结果集 else { return new List { dataTables[0].ToList(returnTypes[0]) }; } } /// /// 将 DataSet 转 特定类型 /// /// DataSet /// 特定类型集合 /// object public static Task> ToListAsync(this DataSet dataSet, params Type[] returnTypes) { return Task.FromResult(dataSet.ToList(returnTypes)); } /// /// 将 DataTable 转 特定类型 /// /// DataTable /// 返回值类型 /// object public static object ToList(this DataTable dataTable, Type returnType) { var isGenericType = returnType.IsGenericType; // 获取类型真实返回类型 var underlyingType = isGenericType ? returnType.GenericTypeArguments.First() : returnType; var resultType = typeof(List<>).MakeGenericType(underlyingType); var list = Activator.CreateInstance(resultType); var addMethod = resultType.GetMethod("Add"); // 将 DataTable 转为行集合 var dataRows = dataTable.AsEnumerable(); // 如果是基元类型 if (underlyingType.IsRichPrimitive()) { // 遍历所有行 foreach (var dataRow in dataRows) { // 只取第一列数据 var firstColumnValue = dataRow[0]; // 转换成目标类型数据 var destValue = firstColumnValue?.ChangeType(underlyingType); // 添加到集合中 _ = addMethod.Invoke(list, new[] { destValue }); } } // 处理Object类型 else if (underlyingType == typeof(object)) { // 获取所有列名 var columns = dataTable.Columns; // 遍历所有行 foreach (var dataRow in dataRows) { var dic = new Dictionary(); foreach (DataColumn column in columns) { dic.Add(column.ColumnName, dataRow[column]); } _ = addMethod.Invoke(list, new[] { dic }); } } else { // 获取所有的数据列和类公开实例属性 var dataColumns = dataTable.Columns; var properties = underlyingType.GetProperties(BindingFlags.Public | BindingFlags.Instance); //.Where(p => !p.IsDefined(typeof(NotMappedAttribute), true)); // sql 数据转换无需判断 [NotMapperd] 特性 // 遍历所有行 foreach (var dataRow in dataRows) { var model = Activator.CreateInstance(underlyingType); // 遍历所有属性并一一赋值 foreach (var property in properties) { // 获取属性对应的真实列名 var columnName = property.Name; if (property.IsDefined(typeof(ColumnAttribute), true)) { var columnAttribute = property.GetCustomAttribute(true); if (!string.IsNullOrWhiteSpace(columnAttribute.Name)) columnName = columnAttribute.Name; } // 如果 DataTable 不包含该列名,则跳过 if (!dataColumns.Contains(columnName)) continue; // 获取列值 var columnValue = dataRow[columnName]; // 如果列值未空,则跳过 if (columnValue == DBNull.Value) continue; // 转换成目标类型数据 var destValue = columnValue?.ChangeType(property.PropertyType); property.SetValue(model, destValue); } // 添加到集合中 _ = addMethod.Invoke(list, new[] { model }); } } return list; } /// /// 将 DataTable 转 特定类型 /// /// DataTable /// 返回值类型 /// object public static Task ToListAsync(this DataTable dataTable, Type returnType) { return Task.FromResult(dataTable.ToList(returnType)); } /// /// 处理元组类型返回值 /// /// 数据集 /// 返回值类型 /// internal static object ToValueTuple(this DataSet dataSet, Type tupleType) { // 获取元组最底层类型 var underlyingTypes = tupleType.GetGenericArguments().Select(u => u.IsGenericType ? u.GetGenericArguments().First() : u); var toListMethod = typeof(DbDataConvertExtensions) .GetMethods(BindingFlags.Public | BindingFlags.Static) .First(u => u.Name == "ToList" && u.IsGenericMethod && u.GetGenericArguments().Length == tupleType.GetGenericArguments().Length) .MakeGenericMethod(underlyingTypes.ToArray()); return toListMethod.Invoke(null, new[] { dataSet }); } } }