コード例 #1
0
            public async Task <List <AllCategoryDto> > GetCategoryTree(List <Category> allCategories, int?parentId = null)
            {
                var categories = new List <AllCategoryDto>();

                var children = allCategories
                               .Where(x => x.ParentCategoryId == parentId)
                               .OrderBy(x => x.Sort)
                               .ToList();

                foreach (var child in children)
                {
                    AllCategoryDto category = new AllCategoryDto
                    {
                        Guid = child.CategoryGuid,
                        //ParentId = child.ParentCategoryId,
                        Title = child.DisplayName,
                        //Order = child.Sort
                    };

                    category.Children = await GetCategoryChildren(allCategories, category);

                    categories.Add(category);
                }

                return(categories);
            }
コード例 #2
0
            private async Task <List <AllCategoryDto> > GetCategoryChildren(List <Category> allCategories, AllCategoryDto category)
            {
                var c = await _context.Category
                        .Where(x => x.CategoryGuid == category.Guid)
                        .SingleOrDefaultAsync();

                if (c != null)
                {
                    var subCategories = allCategories
                                        .Where(x => x.ParentCategoryId == c.CategoryId)
                                        .OrderBy(x => x.Sort)
                                        .Select(x => new AllCategoryDto
                    {
                        Guid = x.CategoryGuid,
                        //ParentId = x.ParentCategoryId,
                        Title = x.DisplayName,
                        //Order = x.Sort
                    }).ToList();

                    if (subCategories != null)
                    {
                        category.Children = subCategories;

                        foreach (var item in category.Children)
                        {
                            item.Children = await GetCategoryChildren(allCategories, item);
                        }
                    }

                    return(category.Children);
                }

                return(null);
            }