.net list turn tree

The main method

 1        /// <summary>
 2         /// 转化成树结构
 3         /// </summary>
 4         /// <param name="menuList">菜单的平级list</param>
 5         /// <returns></returns>
 6         private List<AbpMenuModel> GetTreeParent(List<AbpMenuDto> menuList)
 7         {
 8             var dic = new Dictionary<long, AbpMenuModel>(menuList.Count);
 9             foreach (var chapter in menuList)
10             {
11                 dic.Add(chapter.Id, new AbpMenuModel { Id = chapter.Id, Title = chapter.Title, Path = chapter.Path, ParentId = chapter.ParentId, Icon = chapter.Icon });
12             }
13             foreach (var chapter in dic.Values)
14             {
15                 if (dic.ContainsKey(chapter.ParentId.Value))
16                 {
17                     if (dic[chapter.ParentId.Value].Children == null)
18                     {
19                         dic[chapter.ParentId.Value].Children = new List<AbpMenuModel>();
20 
21                     }
22                     dic[chapter.ParentId.Value].Children.Add(chapter);
23                 }
24             }
25             return dic.Values.Where(t => t.ParentId == 0).ToList();
26         }

 

Before conversion class

    [AutoMapTo(typeof(AbpMenuDto))]
    public class AbpMenuDto : EntityDto<long>
    {
        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// 菜单名
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 图标
        /// </summary>
        public string{Icon GET ; SET ;} 

        ///  <Summary> 
        /// Parent ID
         ///  </ Summary> 
        public  Long ? {The ParentId GET ; SET ;}
         ///  <Summary> 
        /// type menu (menu) elements or (doc) (button or the like)
         ///  </ Summary> 
        public  String the Type { GET ; SET ;}
         ///  <Summary> 
        ///   at the time of the parent doc unique code
         ///  </ Summary> 
        public  String Code { GET ; SET;} 

        ///  <Summary> 
        ///   name when DOC
         ///  </ Summary> 
        public  String the Name { GET ; SET ;} 
    }

 

The converted class

 public class AbpMenuModel : EntityDto<long>
    {
        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// 菜单名
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 图标
        /// </summary>
        public string Icon { get; set;} 

        ///  <Summary> 
        /// Parent ID
         ///  </ Summary> 
        public  Long ? {The ParentId GET ; SET ;}
         ///  <Summary> 
        /// type menu (menu) or elements (doc) (buttons, etc.)
         ///  </ Summary> 
        public  String the Type { GET ; SET ;}
         ///  <Summary> 
        ///   unique code at the time of the parent DOC
         ///  </ Summary> 
        public  String Code { GET ; SET ;} 

        ///  <Summary> 
        ///  doc时 的名称
        /// </summary>
        public string Name { get; set; }

        public List<AbpMenuModel> Children { get; set; }

 

Guess you like

Origin www.cnblogs.com/LmuQuan/p/11233684.html