C#通用递归生成树形结构数据

2021-03-06 07:27

阅读:547

标签:str   HERE   void   name   param   调用   方法   span   idf   

1.首先定义树形结构类

        /// 
        /// 构建树形结构类
        /// 
        public class TreeModel
        {
            public string ID { set; get; }
            public string PARENTID { set; get; }
            public string NAME { set; get; }
            public List TREECHILDREN { set; get; }
        }

2.利用反射将泛型list转换为树形结构类

      /// 
        /// 公用递归(反射转换List)
        /// 
        /// 
        /// 数据列表
        /// 父级ID
        /// id字段名称
        /// 父级id字段名称
        /// name字段名称
        /// 
        public List ConversionList(List allList, string parentId, string idField, string parentIdField, string nameField)
        {
            List list = new List();
            TreeModel model = null;
            foreach (var item in allList)
            {
                model = new TreeModel();
                foreach (System.Reflection.PropertyInfo p in item.GetType().GetProperties())
                {
                    if (p.Name == idField)
                    {
                        model.ID = p.GetValue(item).ToString();
                    }
                    if (p.Name == parentIdField)
                    {
                        model.PARENTID = p.GetValue(item).ToString();
                    }
                    if (p.Name == nameField)
                    {
                        model.NAME = p.GetValue(item).ToString();
                    }
                }
                list.Add(model);
            }
            return OperationParentData(list, parentId);
        }

3.递归方法

     /// 
        /// 公用递归(处理递归最父级数据)
        /// 
        /// 树形列表数据
        /// 父级Id
        /// 
        public List OperationParentData(List treeDataList, string parentId)
        {
            var data = treeDataList.Where(x => x.PARENTID == parentId);
            List list = new List();
            foreach (var item in data)
            {
                OperationChildData(treeDataList, item);
                list.Add(item);
            }
            return list;
        }
        /// 
        /// 公用递归(递归子级数据)
        /// 
        /// 树形列表数据
        /// 父级model
        public void OperationChildData(List treeDataList, TreeModel parentItem)
        {
            var subItems = treeDataList.Where(ee => ee.PARENTID == parentItem.ID).ToList();
            if (subItems.Count != 0)
            {
                parentItem.TREECHILDREN = new List();
                parentItem.TREECHILDREN.AddRange(subItems);
                foreach (var subItem in subItems)
                {
                    OperationChildData(treeDataList, subItem);
                }
            }
        }

4.调用

List treeViewModels = new List();
//list为待处理数据集,pID为父级节点ID;然后分别传入待处理数据集中ID的字段名称,父级ID的字段名称和需要展示Name字段名称
treeViewModels
= ConversionList(list, pID, "ID", "PID", "EFName");

5.处理后数据结果

[{
        "ID": "00000000-0000-0000-0000-000000000001",
        "PARENTID": "00000000-0000-0000-0000-000000000000",
        "NAME": "1级",
        "TREECHILDREN": [{
            "ID": "00000000-0000-0000-0000-000000000002",
            "PARENTID": "00000000-0000-0000-0000-000000000001",
            "NAME": "2级",
            "TREECHILDREN": [{
                "ID": "00000000-0000-0000-0000-000000000003",
                "PARENTID": "00000000-0000-0000-0000-000000000002",
                "NAME": "3级",
                "TREECHILDREN": [{
                    "ID": "00000000-0000-0000-0000-000000000005",
                    "PARENTID": "00000000-0000-0000-0000-000000000003",
                    "NAME": "4级",
                    "TREECHILDREN": null
                }]
            }]
        }]
    },
    {
        "ID": "00000000-0000-0000-0000-000000000004",
        "PARENTID": "00000000-0000-0000-0000-000000000000",
        "NAME": "1级",
        "TREECHILDREN": null
    }
]

 

C#通用递归生成树形结构数据

标签:str   HERE   void   name   param   调用   方法   span   idf   

原文地址:https://www.cnblogs.com/waxwt/p/12872762.html


评论


亲,登录后才可以留言!