mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Support NaviteType.Dynamic one to one
This commit is contained in:
parent
435b889ff2
commit
fc6e5ef8ad
@ -29,6 +29,9 @@ namespace OrmTest
|
|||||||
it);
|
it);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var list3 = db.Queryable<StudentA>()
|
||||||
|
.Includes(it => it.SchoolA.MappingField(z=>z.SchoolId,()=>it.SchoolId).ToList() )
|
||||||
|
.ToList();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
public List<object> GetSetList(object item, List<object> list, List<MappingFieldsExpression> mappingFieldsExpressions)
|
||||||
{
|
{
|
||||||
foreach (var field in mappingFieldsExpressions)
|
foreach (var field in mappingFieldsExpressions)
|
||||||
|
@ -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)
|
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 navEntityInfo = this.Context.EntityMaintenance.GetEntityInfo(navEntity);
|
||||||
var sqlObj = GetWhereSql(navObjectNameColumnInfo.Navigat.Name);
|
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())");
|
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)
|
private SqlInfo GetWhereSql(string properyName=null)
|
||||||
{
|
{
|
||||||
if (_ListCallFunc == null|| _ListCallFunc.Count==0) return new SqlInfo();
|
if (_ListCallFunc == null|| _ListCallFunc.Count==0) return new SqlInfo();
|
||||||
|
@ -24,5 +24,9 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return thisValue;
|
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…
Reference in New Issue
Block a user