Optimized code

This commit is contained in:
sunkaixuan
2024-01-18 05:49:31 +08:00
parent 5d70375d51
commit 409fb61360
2 changed files with 6 additions and 6 deletions

View File

@@ -352,7 +352,7 @@ namespace SqlSugar
{ {
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name; var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
string parentIdName = GetParentName(parentIdExpression); string parentIdName = GetParentName(parentIdExpression);
return UtilMethods.BuildTree(list, pk, parentIdName, childName, rootValue)?.ToList() ?? default; return UtilMethods.BuildTree(this.Context,list, pk, parentIdName, childName, rootValue)?.ToList() ?? default;
} }
private static string GetParentName(Expression<Func<T, object>> parentIdExpression) private static string GetParentName(Expression<Func<T, object>> parentIdExpression)

View File

@@ -19,12 +19,12 @@ namespace SqlSugar
public class UtilMethods public class UtilMethods
{ {
public static IEnumerable<T> BuildTree<T>(IEnumerable<T> list, string idName, string pIdName, string childName, object rootValue) public static IEnumerable<T> BuildTree<T>(ISqlSugarClient db,IEnumerable<T> list, string idName, string pIdName, string childName, object rootValue)
{ {
var type = typeof(T); var entityInfo = db.EntityMaintenance.GetEntityInfo<T>(); ;
var mainIdProp = type.GetProperty(idName); var mainIdProp = entityInfo.Type.GetProperty(idName);
var pIdProp = type.GetProperty(pIdName); var pIdProp = entityInfo.Type.GetProperty(pIdName);
var childProp = type.GetProperty(childName); var childProp = entityInfo.Type.GetProperty(childName);
var kvList = list.ToDictionary(x => mainIdProp.GetValue(x).ObjToString()); var kvList = list.ToDictionary(x => mainIdProp.GetValue(x).ObjToString());
var group = list.GroupBy(x => pIdProp.GetValue(x).ObjToString()); var group = list.GroupBy(x => pIdProp.GetValue(x).ObjToString());