From fc6e5ef8adec09453d3c0c673e5da44f266a444b Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 6 May 2022 12:36:27 +0800 Subject: [PATCH] Support NaviteType.Dynamic one to one --- .../SqlServerTest/UnitTest/UintDynamic.cs | 5 ++- .../QueryableProvider/MappingFieldsHelper.cs | 16 +++++++++ .../QueryableProvider/NavigatManager.cs | 33 ++++++++++++++++++- .../SqlSugar/Utilities/CommonExtensions.cs | 4 +++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UintDynamic.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UintDynamic.cs index 82f462e7d..8b20c7592 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UintDynamic.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UintDynamic.cs @@ -29,7 +29,10 @@ namespace OrmTest it); }); - + var list3 = db.Queryable() + .Includes(it => it.SchoolA.MappingField(z=>z.SchoolId,()=>it.SchoolId).ToList() ) + .ToList(); + } public class StudentA diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs index a5f915ecb..2263ff724 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs @@ -72,6 +72,22 @@ namespace SqlSugar } } + public void SetChildItem(EntityColumnInfo navColumnInfo, object item, List list, List mappingFieldsExpressions) + { + if (item != null) + { + //var expable =Expressionable.Create(); + List setList = GetSetList(item, list, mappingFieldsExpressions); + //navColumnInfo.PropertyInfo.SetValue(); + var instance = Activator.CreateInstance(navColumnInfo.PropertyInfo.PropertyType, true); + var ilist = instance as IList; + foreach (var value in setList) + { + navColumnInfo.PropertyInfo.SetValue(item, value); + } + + } + } public List GetSetList(object item, List list, List mappingFieldsExpressions) { foreach (var field in mappingFieldsExpressions) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs index b41c19352..8e33b8607 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs @@ -342,7 +342,13 @@ namespace SqlSugar private void Dynamic(List list, Func, List> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo) { - var navEntity = navObjectNameColumnInfo.PropertyInfo.PropertyType.GetGenericArguments()[0]; + var args = navObjectNameColumnInfo.PropertyInfo.PropertyType.GetGenericArguments(); + if (args.Length == 0) + { + DynamicOneToOne(list,selector,listItemEntity, navObjectNamePropety, navObjectNameColumnInfo); + return; + } + var navEntity = args[0]; var navEntityInfo = this.Context.EntityMaintenance.GetEntityInfo(navEntity); var sqlObj = GetWhereSql(navObjectNameColumnInfo.Navigat.Name); Check.ExceptionEasy(sqlObj.MappingExpressions.IsNullOrEmpty(), $"Dynamic need MappingField ,Demo: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())", $"自定义映射需要 MappingFields ,例子: Includes(it => it.Books.MappingFields(z=>z.studenId,()=>it.StudentId).ToList())"); @@ -364,6 +370,31 @@ namespace SqlSugar } } + + private void DynamicOneToOne(List list, Func, List> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo) + { + var navEntity = navObjectNameColumnInfo.PropertyInfo.PropertyType; + var navEntityInfo = this.Context.EntityMaintenance.GetEntityInfo(navEntity); + var sqlObj = GetWhereSql(navObjectNameColumnInfo.Navigat.Name); + Check.ExceptionEasy(sqlObj.MappingExpressions.IsNullOrEmpty(), $"Dynamic need MappingField ,Demo: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())", $"自定义映射需要 MappingFields ,例子: Includes(it => it.Books.MappingFields(z=>z.studenId,()=>it.StudentId).ToList())"); + if (list.Any() && navObjectNamePropety.GetValue(list.First()) == null) + { + MappingFieldsHelper helper = new MappingFieldsHelper(); + helper.Context = this.Context; + helper.NavEntity = navEntityInfo; + helper.RootEntity = this.Context.EntityMaintenance.GetEntityInfo(); + var whereSql = helper.GetMppingSql(list, sqlObj.MappingExpressions); + var navList = selector(this.Context.Queryable().AS(navEntityInfo.DbTableName).AddParameters(sqlObj.Parameters).Where(whereSql, true).WhereIF(sqlObj.WhereString.HasValue(), sqlObj.WhereString).Select(sqlObj.SelectString).OrderByIF(sqlObj.OrderByString.HasValue(), sqlObj.OrderByString)); + if (navList.HasValue()) + { + foreach (var item in list) + { + helper.SetChildItem(navObjectNameColumnInfo, item, navList, sqlObj.MappingExpressions); + } + } + } + } + private SqlInfo GetWhereSql(string properyName=null) { if (_ListCallFunc == null|| _ListCallFunc.Count==0) return new SqlInfo(); diff --git a/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs b/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs index b77e8f834..42dbc0373 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs @@ -24,5 +24,9 @@ namespace SqlSugar { return thisValue; } + public static List MappingField(this T thisValue, Func leftField, Func rightField) where T:class + { + return new List() { thisValue }; + } } }