diff --git a/framework/Furion/DatabaseAccessor/Extensions/DatabaseFacade/DbDataConvertExtensions.cs b/framework/Furion/DatabaseAccessor/Extensions/DatabaseFacade/DbDataConvertExtensions.cs index 4247f07e3a6b6c72b467143f0edbb4c43dcdb017..ed9e9b27240d3935ed4ae32e0072ac030286f790 100644 --- a/framework/Furion/DatabaseAccessor/Extensions/DatabaseFacade/DbDataConvertExtensions.cs +++ b/framework/Furion/DatabaseAccessor/Extensions/DatabaseFacade/DbDataConvertExtensions.cs @@ -1,4 +1,4 @@ -using Furion.DependencyInjection; +using Furion.DependencyInjection; using Furion.Extensions; using Mapster; using System; @@ -329,5 +329,51 @@ namespace Furion.DatabaseAccessor { return Task.FromResult(dataTable.ToList(returnType)); } + + #region 把List转换为DataTable + /// + /// 把List转换为DataTable + /// + /// 数据实体模型 + /// 实体列表 + /// 源结构表 + /// + public static DataTable ToDataTable(this ICollection items,DataTable sourceTable) + { + if(sourceTable==null) + sourceTable = new DataTable(); + + PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + foreach (PropertyInfo prop in Props) + { + DataColumn col = new DataColumn(); + col.ColumnName = prop.Name; + col.DataType = prop.PropertyType; + sourceTable.Columns.Add(col); + } + + foreach (T obj in items) + { + //DataRow newRow = sourceTable.NewRow(); + //for (int i = 0; i < Props.Length; i++) + //{ + // PropertyInfo prop = Props[i]; + // if(sourceTable.Columns.Contains(prop.Name)) + // newRow[prop.Name] = Props[i].GetValue(obj, null); + //} + //sourceTable.Rows.Add(newRow); + + //快速模式 + var values = new object[Props.Length]; + for (int i = 0; i < Props.Length; i++) + { + values[i] = Props[i].GetValue(obj, null); + } + sourceTable.Rows.Add(values); + } + + return sourceTable; + } + #endregion } } \ No newline at end of file