From 646e44e1a294749ed25b35cb02f15aa45ad98242 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 11 Dec 2022 17:09:37 +0800 Subject: [PATCH] Synchronization code --- .../SqlSugar/Interface/IContextMethods.cs | 1 + .../SqlSugar/Infrastructure/ContextMethods.cs | 34 +++++++++++++++++++ .../SqlSugar/Interface/IContextMethods.cs | 1 + .../SqlSugar/Utilities/UtilMethods.cs | 6 +++- 4 files changed, 41 insertions(+), 1 deletion(-) 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/SqlSugar/Utilities/UtilMethods.cs index 02a38b5d0..e297fcf47 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -19,7 +19,11 @@ namespace SqlSugar { public static string ToUnderLine(string str, bool isToUpper = false) { - if (isToUpper) + if (str == null || str.Contains("_")) + { + return str; + } + else if (isToUpper) { return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToUpper(); }