Special integration usage includes(x=> x.xx.tolist ())

This commit is contained in:
sunkaixuan
2025-04-02 11:36:28 +08:00
parent 6299757220
commit f9d484959e
5 changed files with 46 additions and 16 deletions

View File

@@ -93,21 +93,9 @@ namespace SqlSugar
private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new() private InsertNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{ {
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
if (expression is LambdaExpression lambda) if (name == null)
{ {
if (lambda.Body is MethodCallExpression method) name =ExpressionTool.GetMemberNameByMethod(expression, name);
{
if (method.Method.Name == "ToList")
{
if (method.Arguments.FirstOrDefault() is { } arg)
{
if (arg is MemberExpression member)
{
name = member.Member.Name;
}
}
}
}
} }
var isRoot = false; var isRoot = false;
if (this._ParentEntity == null) if (this._ParentEntity == null)
@@ -137,7 +125,7 @@ namespace SqlSugar
InitParentList(); InitParentList();
InsertManyToMany<TChild>(name, nav); InsertManyToMany<TChild>(name, nav);
} }
AddContextInfo(name,isRoot); AddContextInfo(name, isRoot);
return GetResult<TChild>(); return GetResult<TChild>();
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@@ -137,6 +138,10 @@ namespace SqlSugar
} }
children.AddRange(childs); children.AddRange(childs);
} }
else if (childs == null && parentNavigateProperty.PropertyInfo.GetValue(item) is IList ilist && ilist != null && ilist.Count > 0)
{
childs = GetIChildsBylList(children, thisFkColumn, parentValue, ilist);
}
ids.Add(parentValue); ids.Add(parentValue);
if (_Options?.OneToManyNoDeleteNull == true && childs == null) if (_Options?.OneToManyNoDeleteNull == true && childs == null)
{ {
@@ -194,6 +199,16 @@ namespace SqlSugar
_NavigateType = null; _NavigateType = null;
SetNewParent<TChild>(thisEntity, thisPkColumn); SetNewParent<TChild>(thisEntity, thisPkColumn);
} }
private static List<TChild> GetIChildsBylList<TChild>(List<TChild> children, EntityColumnInfo thisFkColumn, object parentValue, IList ilist) where TChild : class, new()
{
List<TChild> childs = ilist.Cast<TChild>().ToList();
foreach (var child in childs)
{
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
}
children.AddRange(childs);
return childs;
}
private static bool ParentIsPk(EntityColumnInfo parentNavigateProperty) private static bool ParentIsPk(EntityColumnInfo parentNavigateProperty)
{ {

View File

@@ -97,6 +97,10 @@ namespace SqlSugar
IsFirst = isRoot && this._ParentList == null; IsFirst = isRoot && this._ParentList == null;
InitParentList(); InitParentList();
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
if (name == null)
{
name = ExpressionTool.GetMemberNameByMethod(expression, name);
}
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
if (nav.Navigat == null) if (nav.Navigat == null)
{ {

View File

@@ -10,6 +10,29 @@ namespace SqlSugar
{ {
public class ExpressionTool public class ExpressionTool
{ {
public static string GetMemberNameByMethod(Expression expression, string name)
{
if (expression is LambdaExpression lambda)
{
if (lambda.Body is MethodCallExpression method)
{
if (method.Method.Name == "ToList")
{
if (method.Arguments.FirstOrDefault() is { } arg)
{
if (arg is MemberExpression member)
{
name = member.Member.Name;
}
}
}
}
}
return name;
}
internal static string ResolveMemberValue(ExpressionContext context, Expression item, string value) internal static string ResolveMemberValue(ExpressionContext context, Expression item, string value)
{ {
if (item is MemberExpression member) if (item is MemberExpression member)

View File

@@ -2,7 +2,7 @@
<package > <package >
<metadata> <metadata>
<id>SqlSugarCore</id> <id>SqlSugarCore</id>
<version>5.1.4.188-preview03</version> <version>5.1.4.188-preview09</version>
<authors>sunkaixuan</authors> <authors>sunkaixuan</authors>
<owners>果糖大数据科技</owners> <owners>果糖大数据科技</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl> <licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>