mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-01-23 21:32:04 +08:00
Synchronization code
This commit is contained in:
@@ -352,7 +352,7 @@ namespace SqlSugar
|
||||
{
|
||||
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
||||
string parentIdName = GetParentName(parentIdExpression);
|
||||
return 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)
|
||||
@@ -366,32 +366,6 @@ namespace SqlSugar
|
||||
return parentIdName;
|
||||
}
|
||||
|
||||
private static IEnumerable<T> BuildTree(IEnumerable<T> list, string idName, string pIdName, string childName, object rootValue)
|
||||
{
|
||||
var type = typeof(T);
|
||||
var mainIdProp = type.GetProperty(idName);
|
||||
var pIdProp = type.GetProperty(pIdName);
|
||||
var childProp = type.GetProperty(childName);
|
||||
|
||||
var kvList = list.ToDictionary(x => mainIdProp.GetValue(x).ObjToString());
|
||||
var group = list.GroupBy(x => pIdProp.GetValue(x).ObjToString());
|
||||
|
||||
var root = rootValue != null ? group.FirstOrDefault(x => x.Key == rootValue.ObjToString()) : group.FirstOrDefault(x => x.Key == null || x.Key == "" || x.Key == "0" || x.Key == Guid.Empty.ToString());
|
||||
|
||||
if (root != null)
|
||||
{
|
||||
foreach (var item in group)
|
||||
{
|
||||
if (kvList.TryGetValue(item.Key, out var parent))
|
||||
{
|
||||
childProp.SetValue(parent, item.ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public List<T> GetTreeChildList(List<T> alllist, object pkValue, string pkName, string childName, string parentIdName)
|
||||
{
|
||||
var result = alllist.Where(it =>
|
||||
|
||||
@@ -18,6 +18,40 @@ namespace SqlSugar
|
||||
{
|
||||
public class UtilMethods
|
||||
{
|
||||
|
||||
public static IEnumerable<T> BuildTree<T>(ISqlSugarClient db,IEnumerable<T> list, string idName, string pIdName, string childName, object rootValue)
|
||||
{
|
||||
var entityInfo = db.EntityMaintenance.GetEntityInfo<T>(); ;
|
||||
var mainIdProp = entityInfo.Type.GetProperty(idName);
|
||||
var pIdProp = entityInfo.Type.GetProperty(pIdName);
|
||||
var childProp = entityInfo.Type.GetProperty(childName);
|
||||
|
||||
Dictionary<string, T> kvList;
|
||||
IEnumerable<IGrouping<string, T>> group;
|
||||
BuildTreeGroup(list, mainIdProp, pIdProp, out kvList, out group);
|
||||
|
||||
var root = rootValue != null ? group.FirstOrDefault(x => x.Key == rootValue.ObjToString()) : group.FirstOrDefault(x => x.Key == null || x.Key == "" || x.Key == "0" || x.Key == Guid.Empty.ToString());
|
||||
|
||||
if (root != null)
|
||||
{
|
||||
foreach (var item in group)
|
||||
{
|
||||
if (kvList.TryGetValue(item.Key, out var parent))
|
||||
{
|
||||
childProp.SetValue(parent, item.ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private static void BuildTreeGroup<T>(IEnumerable<T> list, PropertyInfo mainIdProp, PropertyInfo pIdProp, out Dictionary<string, T> kvList, out IEnumerable<IGrouping<string, T>> group)
|
||||
{
|
||||
kvList = list.ToDictionary(x => mainIdProp.GetValue(x).ObjToString());
|
||||
group = list.GroupBy(x => pIdProp.GetValue(x).ObjToString());
|
||||
}
|
||||
|
||||
internal static bool? _IsErrorDecimalString { get; set; }
|
||||
internal static bool? IsErrorDecimalString()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user