Exemplo n.º 1
0
        public CategoryGroup DataGroup(List <CategoryData> datas)
        {
            CategoryGroup       parents = new CategoryGroup();
            List <CategoryData> roots   = datas.Where(s => !s.ParentId.HasValue).ToList();
            List <int>          ids     = roots.Select(s => s.Id).ToList();

            foreach (CategoryData item in roots)
            {
                CategoryGroup root = new CategoryGroup()
                {
                    Root = item
                };
                List <CategoryData> childrens = datas.Where(d => !ids.Contains(d.Id)).ToList();
                CategoryGroup       group     = ForeachData(childrens, item.Id);
                root.Childrens.AddRange(group.Childrens.ToArray());
                parents.Childrens.Add(root);
            }
            return(parents);
        }
Exemplo n.º 2
0
        public CategoryGroup ForeachData(List <CategoryData> items, int parentId)
        {
            CategoryGroup       node      = new CategoryGroup();
            List <CategoryData> childrens = items.Where(s => s.ParentId == parentId).ToList();

            if (childrens.Count == 0)
            { //没有相关子节点
                return(node);
            }
            List <int>          pids = childrens.Select(s => s.Id).ToList();
            List <CategoryData> chs  = items.Where(p => !pids.Contains(p.Id)).ToList();//剔除当前节点的子节点数据

            foreach (CategoryData item in childrens)
            {
                List <CategoryData> cds   = chs.Where(c => c.ParentId == item.Id).ToList();
                CategoryGroup       group = new CategoryGroup()
                {
                    Root = item
                };
                if (cds.Count == 0)
                {
                    node.Childrens.Add(group);
                    continue;
                }

                List <int>          cdsId  = cds.Select(s => s.Id).ToList();
                List <CategoryData> levels = items.Where(s => !cdsId.Contains(s.Id)).ToList();
                foreach (CategoryData cs in cds)
                {
                    CategoryGroup levelnode = new CategoryGroup()
                    {
                        Root = cs
                    };
                    CategoryGroup childern = ForeachData(levels, cs.Id);
                    levelnode.Childrens.AddRange(childern.Childrens.ToArray());
                    group.Childrens.Add(levelnode);
                }
                node.Childrens.Add(group);
            }
            return(node);
        }