Browse Source

Support NaviteType.Dynamic one to one

pull/18/head
sunkaixuan 3 years ago
parent
commit
fc6e5ef8ad
  1. 5
      Src/Asp.Net/SqlServerTest/UnitTest/UintDynamic.cs
  2. 16
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs
  3. 33
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs
  4. 4
      Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs

5
Src/Asp.Net/SqlServerTest/UnitTest/UintDynamic.cs

@ -29,7 +29,10 @@ namespace OrmTest
it);
});
var list3 = db.Queryable<StudentA>()
.Includes(it => it.SchoolA.MappingField(z=>z.SchoolId,()=>it.SchoolId).ToList() )
.ToList();
}
public class StudentA

16
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs

@ -72,6 +72,22 @@ namespace SqlSugar
}
}
public void SetChildItem(EntityColumnInfo navColumnInfo, object item, List<object> list, List<MappingFieldsExpression> mappingFieldsExpressions)
{
if (item != null)
{
//var expable =Expressionable.Create<object>();
List<object> 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<object> GetSetList(object item, List<object> list, List<MappingFieldsExpression> mappingFieldsExpressions)
{
foreach (var field in mappingFieldsExpressions)

33
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs

@ -342,7 +342,13 @@ namespace SqlSugar
private void Dynamic(List<object> list, Func<ISugarQueryable<object>, List<object>> 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<object> list, Func<ISugarQueryable<object>, List<object>> 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<T> helper = new MappingFieldsHelper<T>();
helper.Context = this.Context;
helper.NavEntity = navEntityInfo;
helper.RootEntity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var whereSql = helper.GetMppingSql(list, sqlObj.MappingExpressions);
var navList = selector(this.Context.Queryable<object>().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();

4
Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs

@ -24,5 +24,9 @@ namespace SqlSugar
{
return thisValue;
}
public static List<T> MappingField<T>(this T thisValue, Func<T, object> leftField, Func<object> rightField) where T:class
{
return new List<T>() { thisValue };
}
}
}

Loading…
Cancel
Save