mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-01-22 02:51:58 +08:00
子树平铺查询ToChildList()/ToChildListAsync()性能优化
This commit is contained in:
@@ -2418,33 +2418,37 @@ namespace SqlSugar
|
|||||||
exp = (exp as UnaryExpression).Operand;
|
exp = (exp as UnaryExpression).Operand;
|
||||||
}
|
}
|
||||||
var parentIdName = (exp as MemberExpression).Member.Name;
|
var parentIdName = (exp as MemberExpression).Member.Name;
|
||||||
List<T> result = list.Where(it =>
|
var result = BuildChildList(list, pkName, parentIdName, rootValue);
|
||||||
{
|
|
||||||
var parentValue = it.GetType().GetProperty(parentIdName).GetValue(it);
|
|
||||||
return parentValue.ObjToString() == rootValue.ObjToString();
|
|
||||||
|
|
||||||
}).ToList();
|
|
||||||
if (result != null && result.Count > 0)
|
|
||||||
{
|
|
||||||
List<T> childList = new List<T>();
|
|
||||||
foreach (var item in result)
|
|
||||||
{
|
|
||||||
var pkValue = item.GetType().GetProperty(pkName).GetValue(item);
|
|
||||||
childList.AddRange(GetChildList(parentIdExpression, pkName, list, pkValue,false));
|
|
||||||
}
|
|
||||||
result.AddRange(childList);
|
|
||||||
}
|
|
||||||
if (isRoot)
|
|
||||||
{
|
|
||||||
result.AddRange(list.Where(it =>
|
|
||||||
{
|
|
||||||
var pkValue = it.GetType().GetProperty(pkName).GetValue(it);
|
|
||||||
return pkValue.ObjToString() == rootValue.ObjToString();
|
|
||||||
|
|
||||||
}).ToList());
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<T> BuildChildList(List<T> list, string idName, string pIdName, object rootValue)
|
||||||
|
{
|
||||||
|
var type = typeof(T);
|
||||||
|
var idProp = type.GetProperty(idName);
|
||||||
|
var pIdProp = type.GetProperty(pIdName);
|
||||||
|
|
||||||
|
var kvpList = list.ToDictionary(x => x, v => idProp.GetValue(v).ObjToString());
|
||||||
|
var groupKv = list.GroupBy(x => pIdProp.GetValue(x).ObjToString()).ToDictionary(k => k.Key, v => v.ToList());
|
||||||
|
|
||||||
|
Func<string, List<T>> fc = null;
|
||||||
|
fc = (rootVal) =>
|
||||||
|
{
|
||||||
|
var finalList = new List<T>();
|
||||||
|
if (groupKv.TryGetValue(rootVal, out var nextChildList))
|
||||||
|
{
|
||||||
|
finalList.AddRange(nextChildList);
|
||||||
|
foreach (var child in nextChildList)
|
||||||
|
{
|
||||||
|
finalList.AddRange(fc(kvpList[child]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return finalList;
|
||||||
|
};
|
||||||
|
|
||||||
|
return fc(rootValue.ObjToString());
|
||||||
|
}
|
||||||
|
|
||||||
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list,object rootValue)
|
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list,object rootValue)
|
||||||
{
|
{
|
||||||
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
||||||
|
|||||||
@@ -2418,33 +2418,37 @@ namespace SqlSugar
|
|||||||
exp = (exp as UnaryExpression).Operand;
|
exp = (exp as UnaryExpression).Operand;
|
||||||
}
|
}
|
||||||
var parentIdName = (exp as MemberExpression).Member.Name;
|
var parentIdName = (exp as MemberExpression).Member.Name;
|
||||||
List<T> result = list.Where(it =>
|
var result = BuildChildList(list, pkName, parentIdName, rootValue);
|
||||||
{
|
|
||||||
var parentValue = it.GetType().GetProperty(parentIdName).GetValue(it);
|
|
||||||
return parentValue.ObjToString() == rootValue.ObjToString();
|
|
||||||
|
|
||||||
}).ToList();
|
|
||||||
if (result != null && result.Count > 0)
|
|
||||||
{
|
|
||||||
List<T> childList = new List<T>();
|
|
||||||
foreach (var item in result)
|
|
||||||
{
|
|
||||||
var pkValue = item.GetType().GetProperty(pkName).GetValue(item);
|
|
||||||
childList.AddRange(GetChildList(parentIdExpression, pkName, list, pkValue,false));
|
|
||||||
}
|
|
||||||
result.AddRange(childList);
|
|
||||||
}
|
|
||||||
if (isRoot)
|
|
||||||
{
|
|
||||||
result.AddRange(list.Where(it =>
|
|
||||||
{
|
|
||||||
var pkValue = it.GetType().GetProperty(pkName).GetValue(it);
|
|
||||||
return pkValue.ObjToString() == rootValue.ObjToString();
|
|
||||||
|
|
||||||
}).ToList());
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<T> BuildChildList(List<T> list, string idName, string pIdName, object rootValue)
|
||||||
|
{
|
||||||
|
var type = typeof(T);
|
||||||
|
var idProp = type.GetProperty(idName);
|
||||||
|
var pIdProp = type.GetProperty(pIdName);
|
||||||
|
|
||||||
|
var kvpList = list.ToDictionary(x => x, v => idProp.GetValue(v).ObjToString());
|
||||||
|
var groupKv = list.GroupBy(x => pIdProp.GetValue(x).ObjToString()).ToDictionary(k => k.Key, v => v.ToList());
|
||||||
|
|
||||||
|
Func<string, List<T>> fc = null;
|
||||||
|
fc = (rootValue) =>
|
||||||
|
{
|
||||||
|
var finalList = new List<T>();
|
||||||
|
if (groupKv.TryGetValue(rootValue, out var nextChildList))
|
||||||
|
{
|
||||||
|
finalList.AddRange(nextChildList);
|
||||||
|
foreach (var child in nextChildList)
|
||||||
|
{
|
||||||
|
finalList.AddRange(fc(kvpList[child]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return finalList;
|
||||||
|
};
|
||||||
|
|
||||||
|
return fc(rootValue.ObjToString());
|
||||||
|
}
|
||||||
|
|
||||||
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list,object rootValue)
|
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list,object rootValue)
|
||||||
{
|
{
|
||||||
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
||||||
|
|||||||
Reference in New Issue
Block a user