menuRecursion.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using JLHHJSvr.Com.Model;
  5. namespace JLHHJSvr.LJFramework.Tools
  6. {
  7. public class menuRecursion
  8. {
  9. /// <summary>
  10. /// 递归查询
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. /// <param name="TList">要查找的全部内容</param>
  14. /// <param name="pkName">要查找内容的主ID,名称,父ID</param>
  15. /// <param name="ReList">输出结构</param>
  16. public void GetRecursions<T>(List<T> TList, PkName pkName, List<Recursion2> ReList, List<int> idList)
  17. {
  18. //if (pkName.MainField == string.Empty || pkName.MainName == string.Empty || pkName.ParentField == string.Empty)
  19. //{
  20. // return;
  21. //}
  22. var NTList = TList.Where(t => idList.IndexOf((int)t.GetType().GetProperty(pkName.MainField).GetValue(t)) == -1).ToList();
  23. foreach (var mx in ReList)
  24. {
  25. var parentid = mx.value;
  26. var tmpList = NTList.Where(t => (int)t.GetType().GetProperty(pkName.ParentField).GetValue(t) == parentid).ToList();
  27. if (tmpList.Count > 0)
  28. {
  29. mx.children = new List<Recursion2>();
  30. foreach (var tmp in tmpList)
  31. {
  32. var Child = new Recursion2()
  33. {
  34. text = Convert.ToString(tmp.GetType().GetProperty(pkName.MainName).GetValue(tmp)),
  35. value = Convert.ToInt32(tmp.GetType().GetProperty(pkName.MainField).GetValue(tmp))
  36. };
  37. mx.children.Add(Child);
  38. idList.Add(Child.value);
  39. }
  40. GetRecursions(TList, pkName, mx.children, idList);
  41. }
  42. }
  43. }
  44. }
  45. }