足力健后端,使用.netcore版本,合并1个项目使用

TreeQuery.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace System.Common
  6. {
  7. public static class TreeQuery
  8. {
  9. public static List<T> TreeWhere<T>(this List<T> entityList, Predicate<T> condition, string keyValue = "F_Id", string parentId = "F_ParentId") where T : class
  10. {
  11. List<T> locateList = entityList.FindAll(condition);
  12. var parameter = Expression.Parameter(typeof(T), "t");
  13. List<T> treeList = new List<T>();
  14. foreach (T entity in locateList)
  15. {
  16. treeList.Add(entity);
  17. int pid = int.Parse(entity.GetType().GetProperty(parentId).GetValue(entity, null).ToString());
  18. //string pId = entity.GetType().GetProperty(parentId).GetValue(entity, null).ToString();
  19. while (true)
  20. {
  21. if(pid<=0)
  22. { break; }
  23. //if (string.IsNullOrEmpty(pId) && pId == "0")
  24. //{
  25. // break;
  26. //}
  27. //Predicate<T> upLambda = (Expression.Equal(parameter.Property(keyValue), Expression.Constant(pId))).ToLambda<Predicate<T>>(parameter).Compile();
  28. Predicate<T> upLambda = (Expression.Equal(parameter.Property(keyValue), Expression.Constant(pid))).ToLambda<Predicate<T>>(parameter).Compile();
  29. T upRecord = entityList.Find(upLambda);
  30. if (upRecord != null)
  31. {
  32. treeList.Add(upRecord);
  33. //pId = upRecord.GetType().GetProperty(parentId).GetValue(upRecord, null).ToString();
  34. pid = int.Parse(upRecord.GetType().GetProperty(parentId).GetValue(upRecord, null).ToString());
  35. }
  36. else
  37. {
  38. break;
  39. }
  40. }
  41. }
  42. return treeList.Distinct().ToList();
  43. }
  44. }
  45. }