Update Nav query+Ownsone

This commit is contained in:
sunkaixuan
2025-04-28 17:13:58 +08:00
parent aec7ec3b1c
commit 8e5f8fa2dc

View File

@@ -371,7 +371,21 @@ namespace SqlSugar
{
Check.ExceptionEasy($"{navObjectNamePropety.Name} type error ", $"一对一不能是List对象 {navObjectNamePropety.Name} ");
}
var ids = list.Select(it => it.GetType().GetProperty(navObjectNameColumnInfo.Navigat.Name).GetValue(it)).Select(it => it == null ? "null" : it).Distinct().ToList();
List<object> ids = null;
var isOwnsOneProperty = IsOwnsOneProperty(listItemEntity, navObjectNameColumnInfo);
if (isOwnsOneProperty)
{
var data = listItemEntity.Columns.FirstOrDefault(it => it.PropertyName == navObjectNameColumnInfo.Navigat.Name);
ids = list.Select(it =>
{
var ownsObj = data.ForOwnsOnePropertyInfo.GetValue(it);
return ownsObj.GetType().GetProperty(navObjectNameColumnInfo.Navigat.Name).GetValue(ownsObj);
}).Select(it => it == null ? "null" : it).Distinct().ToList();
}
else
{
ids = list.Select(it => it.GetType().GetProperty(navObjectNameColumnInfo.Navigat.Name).GetValue(it)).Select(it => it == null ? "null" : it).Distinct().ToList();
}
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
if (IsEnumNumber(navColumn))
{
@@ -417,6 +431,46 @@ namespace SqlSugar
.WhereIF(sqlObj.WhereString.HasValue(), sqlObj.WhereString)
.AddParameters(sqlObj.Parameters).Where(conditionalModels)
.Select(sqlObj.SelectString));
if (isOwnsOneProperty)
{
{
// 有 OwnsOne 的情况
var data = listItemEntity.Columns.FirstOrDefault(it => it.PropertyName == navObjectNameColumnInfo.Navigat.Name);
var groupQuery = (from l in list
let ownsObj = data.ForOwnsOnePropertyInfo.GetValue(l)
join n in navList
on ownsObj.GetType()
.GetProperty(navObjectNameColumnInfo.Navigat.Name)
.GetValue(ownsObj)
.ObjToString()
equals navPkColumn.PropertyInfo.GetValue(n).ObjToString()
select new
{
l,
n
}).ToList();
foreach (var item in groupQuery)
{
// var setValue = navList.FirstOrDefault(x => navPkColumn.PropertyInfo.GetValue(x).ObjToString() == navColumn.PropertyInfo.GetValue(item).ObjToString());
if (navObjectNamePropety.GetValue(item.l) == null)
{
navObjectNamePropety.SetValue(item.l, item.n);
}
else
{
//The reserved
}
}
}
}
else
{
var groupQuery = (from l in list
join n in navList
on navColumn.PropertyInfo.GetValue(l).ObjToString()
@@ -443,6 +497,7 @@ namespace SqlSugar
}
}
}
}
private void OneToMany(List<object> list, Func<ISugarQueryable<object>, List<object>> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo)
{
@@ -961,6 +1016,10 @@ namespace SqlSugar
navPkColumn?.SqlParameterDbType == null &&
this.Context?.CurrentConnectionConfig?.MoreSettings?.TableEnumIsString != true;
}
private static bool IsOwnsOneProperty(EntityInfo listItemEntity, EntityColumnInfo navObjectNameColumnInfo)
{
return listItemEntity.Columns.Any(it => it.IsOwnsOne) && !listItemEntity.Type.GetProperties().Any(it => it.PropertyType.Name == navObjectNameColumnInfo.Navigat.Name);
}
private static bool IsJsonMapping(EntityColumnInfo navObjectNameColumnInfo, SqlInfo sqlObj)
{