public RedirectToRouteResult Filter(ProductFilter thefilter, string ProducerStr = null) { TempData["filter"] = thefilter; return RedirectToAction("ShowListPage", "MusicInstruments", new { category = thefilter.category }); }
//Задать начальные значения фильтра private ProductFilter InitializeFilter(int cat) { IEnumerable<Product> products = repository.musicInstruments //товары из выбраной категррии .Where(vm => cat == 0 || vm.Category_Id == cat).ToList(); ProductFilter filter = new ProductFilter() { category = cat, producers = products.Select(x => new ProducerVM { Id = x.producer.Id, Name = x.producer.Name, IsChecked = false }).GroupBy(x => x.Name).Select(x => x.First()).ToList() , colors = products.Select(x => new ColorVM { Id = x.color.Id, Name = x.color.Name, IsChecked = false }).GroupBy(x => x.Name).Select(x => x.First()).ToList(), minPrice = products.Min(x => x.General.Price), maxPrice = products.Max(x => x.General.Price) }; return filter; }
//Фильтрация товаров: private IEnumerable<Product> GoFilter(IEnumerable<Product> filtred, ProductFilter filter) { //По производителю: int[] targetProdIds = filter.producers.Where(x => x.IsChecked).Select(x => x.Id).ToArray(); if(targetProdIds.Count() != 0) filtred = filtred.Where(prod => targetProdIds.Contains(prod.Producer_Id)); //По цвету: int[] targetColIds = filter.colors.Where(x => x.IsChecked).Select(x => x.Id).ToArray(); if (targetColIds.Count() != 0) filtred = filtred.Where(prod => targetColIds.Contains(prod.Color_Id)); //По цене: filtred = filtred.Where(x => x.General.Price >= filter.minPrice); filtred = filtred.Where(x => x.General.Price <= filter.maxPrice); return filtred; }