C# 把带有父子关系的数据转化为------树形结构的数据 ,以及 找出父子级关系的数据中里面的根数据Id

紧接上一篇,将List<Menu>的扁平结构数据, 转换成树形结构的数据 返回给前端   ,   废话不多说,开撸!

---------------------

步骤:

1. 建 Menu实体结构

  public class Menu
    {
        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 菜单名
        /// </summary>
        public string MenuName { get; set; }
        /// <summary>
        /// 父菜单
        /// </summary>
        public int ParentID { get; set; }
    }

 

2. 建Tree 的数据结构(用来做树形结构的数据返回)

    public class Tree
    {
        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 菜单名
        /// </summary>
        public string MenuName { get; set; }
        /// <summary>
        /// 父菜单
        /// </summary>
        public int ParentID { get; set; }


        /// <summary>
        /// 子节点集合
        /// </summary>
        public List<Tree> Children { get; set; }
    }

 

3. 写方法,递归遍历,将Menu实体值赋值给Tree

        //根据父节点获取子节点
        public static List<Tree> GetChildTree(List<Menu> list, int Id)
        {

            List<Tree> tree = new List<Tree>();
            List<Menu> ChildList = GetChildList(list, Id);
            foreach (var item in ChildList)
            {
                Tree treeB = new Tree();
                treeB.ID = item.ID;
                treeB.MenuName = item.MenuName;
                treeB.Children = GetChildTree(list,item.ID);
                tree.Add(treeB);
            }
            return tree;
        }
        
        public static List<Menu> GetChildList(List<Menu> list,int Id)
        {
            var childList = list.Where(x => x.ParentID == Id).ToList();
            return childList;
        }

4. 准备数据,方法调用

            // 准备要处理的数据
            List<Menu> listB = new List<Menu>();
            listB.Add(new Menu { ID = 1, MenuName = "菜单1", ParentID = 0 });
            listB.Add(new Menu { ID = 2, MenuName = "菜单1.1", ParentID = 1 });
            listB.Add(new Menu { ID = 3, MenuName = "菜单1.1.1", ParentID = 2 });
            listB.Add(new Menu { ID = 4, MenuName = "菜单1.1.2", ParentID = 2 });
            listB.Add(new Menu { ID = 5, MenuName = "菜单1.2", ParentID = 1 });
            listB.Add(new Menu { ID = 6, MenuName = "菜单1.2.2", ParentID = 5 });
            listB.Add(new Menu { ID = 7, MenuName = "菜单2", ParentID = 0 });

            var result = GetChildTree(listB, 0);
            string jsonB = new JavaScriptSerializer().Serialize(result);

5. 转换后的树形结构数据结果图示

 

 

 -----------------------开发过程中遇到的问题---------------------------------

 从别人的博客看到这种方式,很高兴,以为改改,很快就可以实现工作中的功能,结果发现还欠缺点东西,就是要传入的父节点Id值给定的是0  ,写死的。

 

而我要传入的这个Id值要是动态的,要根据传入的List集合,找出这个集合数据里面的根节点的Id值。  在这上面的代码中并没有给出, 于是我开始折腾,最终从别人的js 代码中找到了别人的解决思路。 

 

 

我的解决方法如下,希望也能够帮助一些人:

            // 准备要处理的数据
            List<Menu> listB = new List<Menu>();
            listB.Add(new Menu { ID = 1, MenuName = "菜单1", ParentID = 0 });
            listB.Add(new Menu { ID = 2, MenuName = "菜单1.1", ParentID = 1 });
            listB.Add(new Menu { ID = 3, MenuName = "菜单1.1.1", ParentID = 2 });
            listB.Add(new Menu { ID = 4, MenuName = "菜单1.1.2", ParentID = 2 });
            listB.Add(new Menu { ID = 5, MenuName = "菜单1.2", ParentID = 1 });
            listB.Add(new Menu { ID = 6, MenuName = "菜单1.2.2", ParentID = 5 });
            listB.Add(new Menu { ID = 7, MenuName = "菜单2", ParentID = 0 });
            //找出集合里面的根节点的Id
            HashSet<int> parentIds = new HashSet<int>();
            HashSet<int> childIds = new HashSet<int>();
            foreach (var item in listB)
            {
                childIds.Add(item.ID);
                parentIds.Add(item.ParentID);
            }
            parentIds.ExceptWith(childIds);
            int rootId = parentIds.First();


            var result = GetChildTree(listB, rootId);

   

   最后,发表一下感慨,C# 写的代码真的少,7、8行就解决了!

posted @ 2019-12-27 16:47  明志德道  阅读(2156)  评论(1编辑  收藏  举报