diff --git a/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs b/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs index 098162782..e9c43a797 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs @@ -34,6 +34,7 @@ namespace SqlSugar dynamic DataTableToDynamic(DataTable table); List DataTableToList(DataTable table); DataTable ListToDataTable(List list); + DataTable ListToDataTableWithAttr(List list); Dictionary DataTableToDictionary(DataTable table); List> DataTableToDictionaryList(DataTable table); ICacheService GetReflectionInoCacheInstance(); diff --git a/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs index 129171124..14d790b8c 100644 --- a/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs @@ -816,6 +816,40 @@ namespace SqlSugar } return result; } + + public DataTable ListToDataTableWithAttr(List list) + { + var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(); + DataTable result = new DataTable(); + result.TableName = entityInfo.DbTableName; + if (list != null && list.Count > 0) + { + var colimnInfos = entityInfo.Columns.Where(it=>it.IsIgnore==false); + foreach (var pi in colimnInfos) + { + //获取类型 + Type colType = pi.PropertyInfo.PropertyType; + //当类型为Nullable<>时 + if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) + { + colType = colType.GetGenericArguments()[0]; + } + result.Columns.Add(pi.DbColumnName, colType); + } + for (int i = 0; i < list.Count; i++) + { + ArrayList tempList = new ArrayList(); + foreach (var pi in colimnInfos) + { + object obj = pi.PropertyInfo.GetValue(list[i], null); + tempList.Add(obj); + } + object[] array = tempList.ToArray(); + result.LoadDataRow(array, true); + } + } + return result; + } public Dictionary DataTableToDictionary(DataTable table) { return table.Rows.Cast().ToDictionary(x => x[0].ToString(), x => x[1]); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IContextMethods.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IContextMethods.cs index 098162782..e9c43a797 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IContextMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IContextMethods.cs @@ -34,6 +34,7 @@ namespace SqlSugar dynamic DataTableToDynamic(DataTable table); List DataTableToList(DataTable table); DataTable ListToDataTable(List list); + DataTable ListToDataTableWithAttr(List list); Dictionary DataTableToDictionary(DataTable table); List> DataTableToDictionaryList(DataTable table); ICacheService GetReflectionInoCacheInstance(); diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/Sql