码迷,mamicode.com
首页 > 其他好文 > 详细

编写一个通用递归获取树形结构对象集合的方法

时间:2018-11-25 13:21:53      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:col   子节点   HERE   条件   class   pid   vat   color   etl   

        /// <summary>
        /// 通用递归获取树状子节点信息
        /// </summary>
        /// <param name="item"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<T> getTreeListByPid<T, F>(List<F> item, long pid, F d) where T : BaseTreeDto<T>, new() where F : BaseTreeDic, new()
        {
            List<T> ItemsDto = new List<T>();
            //限制条件跳出
            if (item.Where(s => s.Pid == pid).Count() > 0)
            {
                item.Where(s => s.Pid == pid).ToList().ForEach(f =>
                {
                    ItemsDto.Add(new T { Id = f.Id, Name = f.Name, Item = getTreeListByPid<T, F>(item, f.Id, f) });
                });
            }
            return ItemsDto;
        }

 

    public class BaseTreeDto<T>
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public List<T> Item { get; set; }
    }

    public class BaseTreeDic
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        public long Pid { get; set; }
    }

 调用方式 将头节点筛选出来然后传参进入方法即可(以下是伪代码)

           
List<T> list = new List<T>(); List<F> item = GetListF(); List<F> tempAllitem = item.Where(i => i.Pid == 0).ToList(); tempAllitem.ForEach(i => { list.Add(new T{ Id = i.Id, Name = i.Name, Item = getTreeListByPid<T, F>(item, i.Id, i) }); });

 

编写一个通用递归获取树形结构对象集合的方法

标签:col   子节点   HERE   条件   class   pid   vat   color   etl   

原文地址:https://www.cnblogs.com/zzlblog/p/10014907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!