zoukankan      html  css  js  c++  java
  • C# 不用递归,获取无限层级数据

    对象属性

     public class ResList
        {
            public int ID { get; set; }
            public List<ResList> Child { get; set; } = null;
            public int Parent { get; set; }
            public int Rank { get; set; }
        }
     

      数据就是那种有父级ID的那种

     1             List<ResList> reslist = new List<ResList>();//新的数据
     2             List<ResList> alllist = new List<ResList>();//原始数据
     3             //加载原始数据
     4             for (int i = 1; i < 10000; i++)
     5             {
     6                 int j = 0;
     7                 while (Math.Pow(10, j) <= i)
     8                 {
     9                     j++;
    10                 }
    11 
    12                 alllist.Add(new ResList() { ID = i, Parent = i / 10, Rank = j });
    13             }
    14             //加载原始数据完成
    15 
    16            
    17             //下面是处理方法
    18             Dictionary<int, ResList> dic = new Dictionary<int, ResList>();
    19 
    20             foreach (ResList res in alllist)
    21             {
    22                 dic.Add(res.ID, res);
    23 
    24             }
    25             foreach (ResList res in dic.Values)
    26             {
    27                 if (dic.ContainsKey(res.Parent))
    28                 {
    29                     if (dic[res.Parent].Child == null)
    30                     {
    31                         dic[res.Parent].Child = new List<ResList>();
    32                     }
    33                     dic[res.Parent].Child.Add(res);
    34                 }
    35             }
    36             //得到最终的数据List
    37             reslist = dic.Values.Where(t => t.Parent == 1).ToList();        
     

      该方法来源  https://blog.csdn.net/u010162297/article/details/53019101

  • 相关阅读:
    #与javascript:void(0)的区别
    单选框、复选框、下拉列表
    数据类型后的“?”
    c#中日期的处理
    日期控件html
    javascript获取后台传来的json
    Hashtable语法简介
    Hashtable(哈希表)
    Dictionary 字典
    远程SQL Server连接不上
  • 原文地址:https://www.cnblogs.com/wang0020/p/10062116.html
Copyright © 2011-2022 走看看