QueryTree.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using LeaRun.Util.Attributes;
  2. using LeaRun.Util.Extension;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Text;
  10. namespace LeaRun.Util.WebControl
  11. {
  12. /// <summary>
  13. /// 树形结构查询
  14. /// </summary>
  15. public static class QueryTree
  16. {
  17. /// <summary>
  18. /// 树形查询条件
  19. /// </summary>
  20. /// <param name="entityList">数据源</param>
  21. /// <param name="condition">查询条件</param>
  22. /// <param name="primaryKey">主键</param>
  23. /// <param name="parentId"></param>
  24. /// <returns></returns>
  25. public static List<T> TreeWhere<T>(this List<T> entityList, Predicate<T> condition, string primaryKey, string parentId = "ParentId") where T : class
  26. {
  27. List<T> locateList = entityList.FindAll(condition);
  28. var parameter = Expression.Parameter(typeof(T), "t");
  29. //模糊查询表达式
  30. List<T> treeList = new List<T>();
  31. foreach (T entity in locateList)
  32. {
  33. //先把自己加入进来
  34. treeList.Add(entity);
  35. //向上查询
  36. string pId = entity.GetType().GetProperty(parentId).GetValue(entity, null).ToString();
  37. while (true)
  38. {
  39. if (string.IsNullOrEmpty(pId) && pId == "0")
  40. {
  41. break;
  42. }
  43. Predicate<T> upLambda = (Expression.Equal(parameter.Property(primaryKey), Expression.Constant(pId))).ToLambda<Predicate<T>>(parameter).Compile();
  44. T upRecord = entityList.Find(upLambda);
  45. if (upRecord != null)
  46. {
  47. treeList.Add(upRecord);
  48. pId = upRecord.GetType().GetProperty(parentId).GetValue(upRecord, null).ToString();
  49. }
  50. else
  51. {
  52. break;
  53. }
  54. }
  55. }
  56. return treeList.Distinct().ToList();
  57. }
  58. /// <summary>
  59. /// 树形查询条件
  60. /// </summary>
  61. /// <param name="entityList">数据源</param>
  62. /// <param name="condition">查询条件</param>
  63. /// <param name="primaryKey">主键</param>
  64. /// <returns></returns>
  65. public static DataTable TreeWhere(this DataTable table, string condition, string primaryKey)
  66. {
  67. DataRow[] drs = table.Select(condition);
  68. DataTable treeTable = table.Clone();
  69. foreach (DataRow dr in drs)
  70. {
  71. treeTable.ImportRow(dr);
  72. string pId = dr["ParentId"].ToString();
  73. while (true)
  74. {
  75. if (string.IsNullOrEmpty(pId) && pId == "0")
  76. {
  77. break;
  78. }
  79. DataRow[] pdrs = table.Select(primaryKey + "='" + pId + "'");
  80. if (pdrs.Length > 0)
  81. {
  82. treeTable.ImportRow(pdrs[0]);
  83. pId = pdrs[0]["ParentId"].ToString();
  84. }
  85. else
  86. {
  87. break;
  88. }
  89. }
  90. }
  91. DataView treeView = treeTable.DefaultView;
  92. return treeView.ToTable(true);
  93. }
  94. }
  95. }