public IViewComponentResult Invoke(int categoryId, ProductSearchFiltersViewModel viewModel) { var catProducts = context.Products .AsNoTracking() .Include(p => p.Manufacturer) .Include(p => p.ProductSpecificationValues) .ThenInclude(psv => psv.ProductSpecification) .ThenInclude(ps => ps.ProductSpecificationNumbersInOrder) .Where(p => p.CategoryId == categoryId); var prodSpecs = catProducts .SelectMany(p => p.ProductSpecificationValues .Where(psv => psv.ProductSpecification.Name != "Дополнительно" && psv.Value != "-") .Select(psv => psv.ProductSpecification)) .Distinct() .OrderBy(ps => ps.ProductSpecificationNumbersInOrder .First(psn => psn.ProductSpecificationId == ps.Id).NumberInOrder ).ToList(); foreach (var item in prodSpecs) { var prodSVs = context.ProductSpecificationsValues .AsNoTracking() .Include(psv => psv.Product) .GroupBy(psv => new { psv.Product.CategoryId, psv.ProductSpecificationId, psv.Value }) .Where(g => g.Key.ProductSpecificationId == item.Id && g.Key.CategoryId == categoryId && g.Key.Value != "-") .Select(g => g.First()) .ToList(); item.ProductSpecificationValues.AddRange(prodSVs); } if (viewModel.ProdSpecsWithValues == null) { viewModel = new ProductSearchFiltersViewModel { ProductSpecifications = prodSpecs.ToDictionary(ps => ps.Id), ProdSpecsWithValues = prodSpecs .ToDictionary(ps => ps.Id, ps => ps.ProductSpecificationValues .ToDictionary(psv => psv.Id, psv => new ProductSpecificationValueViewModel { ProductSpecificationValue = psv }) ), Manufacturers = catProducts .Select(p => p.Manufacturer) .Distinct() .ToDictionary(m => m.Id, m => new ManufacturerViewModel { Manufacturer = m }), MinProdPrice = catProducts.Min(p => p.Price), MaxProdPrice = catProducts.Max(p => p.Price) }; } else { viewModel.ProdSpecsWithValues = prodSpecs .ToDictionary(ps => ps.Id, ps => ps.ProductSpecificationValues .ToDictionary(psv => psv.Id, psv => new ProductSpecificationValueViewModel { ProductSpecificationValue = psv, IsSelected = viewModel.ProdSpecsWithValues[ps.Id][psv.Id].IsSelected }) ); viewModel.Manufacturers = catProducts .Select(p => p.Manufacturer) .Distinct() .ToDictionary(m => m.Id, m => new ManufacturerViewModel { Manufacturer = m, IsSelected = viewModel.Manufacturers[m.Id].IsSelected }); } // if return(View(viewModel)); }
public async Task <IActionResult> Index(string category, ProductSearchFiltersViewModel filtersViewModel, int?page) { ViewBag.FiltersViewModel = filtersViewModel; var products = context.Products .Include(p => p.Manufacturer) .Where(p => category == null || p.Category.NameForUrl == category); if (filtersViewModel.MaxProdPrice > 0) { products = products .Where(p => p.Price >= filtersViewModel.MinProdPrice && p.Price <= filtersViewModel.MaxProdPrice); } // if if (filtersViewModel.Manufacturers != null && filtersViewModel.Manufacturers.Values.Any(m => m.IsSelected)) { products = products .Where(p => filtersViewModel.Manufacturers .Any(m => m.Key == p.ManufacturerId && m.Value.IsSelected) ); } // if if (filtersViewModel.ProdSpecsWithValues != null && filtersViewModel.ProdSpecsWithValues.Values .Any(pswv => pswv.Values.Any(psv => psv.IsSelected))) { foreach (var item in filtersViewModel.ProdSpecsWithValues) { products = products .Where(p => p.ProductSpecificationValues .Any(pPsv => pPsv.ProductSpecificationId == item.Key && item.Value.Any(psv => psv.Key == pPsv.Id && psv.Value.IsSelected) ) ); } // foreach } // if var viewModel = new ProductsListViewModel { Products = await products.OrderBy(p => p.Id) .Skip(((page ?? 1) - 1) * pageSize) .Take(pageSize) .ToListAsync(), PagingInfo = new PagingInfo { CurrentPage = page ?? 1, ItemsPerPage = pageSize, TotalItems = await products.CountAsync(), ElementSizeClass = "pagination-lg" }, CurrentCategory = context.Categories .FirstOrDefault(c => c.NameForUrl == category) }; ViewBag.SelectedCategory = viewModel.CurrentCategory; viewModel.PagingInfo.PageUrlValues["category"] = category; return(View(viewModel)); }