예제 #1
0
        private List<CategoryViewModel> GetChildrenCategoryByCategory(Category selectedCategory, List<Product> products, IEnumerable<Category> children)
        {
            var categoryViewModel = new List<CategoryViewModel>();
            foreach (var child in children)
            {
                int total;
                var childCategoryViewModels = new List<CategoryViewModel>();

                var child1 = child;
                var childChildCategories = _categories.Where(c => c.ParentCategory == child1).ToList();
                var hasChild = string.Empty;
                if (!childChildCategories.Any())
                {
                    total = products.Count(p => p.CategoryId == child.Id);
                }
                else
                {
                    var childrenCategoryViewModels = GetChildrenCategoryByCategory(selectedCategory, products, childChildCategories);
                    total = childrenCategoryViewModels.Sum(c => c.Total);
                    hasChild = "haschild";
                }

                var childViewModel = new CategoryViewModel
                {
                    Id = child.Id.ToString(),
                    Name = child.Name,
                    Active = selectedCategory.Id == child.Id ? "active" : string.Empty,
                    HasChild = hasChild,
                    Total = total,
                    ChildCategoryViewModels = childCategoryViewModels
                };

                categoryViewModel.Add(childViewModel);
            }
            return categoryViewModel;
        }
예제 #2
0
        private IEnumerable<CategoryViewModel> GetCategoriesByCategory(Category selectCategory, List<Product> products)
        {
            var categoryViewModels = new List<CategoryViewModel>();
            foreach (var category in _categories.Where(c=>c.ParentCategory==null))
            {
                var category1 = category;
                var children = _categories.Where(c=>c.ParentCategory==category1);
                var childrenCategoryViewModel = GetChildrenCategoryByCategory(selectCategory, products, children);
                var categoryViewModel = new CategoryViewModel
                {
                    Id = category.Id.ToString(),
                    Name = category.Name,
                    Active =
                        selectCategory.Id == category.Id
                            ? "active"
                            : (childrenCategoryViewModel.Any(c => c.Active == "active")
                                ? "active"
                                : string.Empty),
                    HasChild = _categories.Any(c => c.ParentCategory == category) ? "haschild" : string.Empty,
                    Total = products.Count(p=>p.CategoryId==category.Id)+childrenCategoryViewModel.Sum(c => c.Total),
                    ChildCategoryViewModels = childrenCategoryViewModel
                };
                categoryViewModels.Add(categoryViewModel);
            }

            return categoryViewModels;
        }