public async Task <FilterProductsDTO> FilterProducts(FilterProductsDTO filter) { var productsQuery = productRepository.GetEntitiesQuery().AsQueryable(); switch (filter.OrderBy) { case ProductOrderBy.PriceAsc: productsQuery = productsQuery.OrderBy(s => s.Price); break; case ProductOrderBy.PriceDec: productsQuery = productsQuery.OrderByDescending(s => s.Price); break; } if (!string.IsNullOrEmpty(filter.Title)) { productsQuery = productsQuery.Where(s => s.ProductName.Contains(filter.Title)); } if (filter.StartPrice != 0) { productsQuery = productsQuery.Where(s => s.Price >= filter.StartPrice); } if (filter.EndPrice != 0) { productsQuery = productsQuery.Where(s => s.Price <= filter.EndPrice); } productsQuery = productsQuery.Where(s => s.Price >= filter.StartPrice); if (filter.Categories != null && filter.Categories.Any()) { productsQuery = productsQuery.SelectMany(s => s.ProductSelectedCategories.Where(f => filter.Categories.Contains(f.ProductCategoryId)).Select(t => t.Product)); } if (filter.EndPrice != 0) { productsQuery = productsQuery.Where(s => s.Price <= filter.EndPrice); } var count = (int)Math.Ceiling(productsQuery.Count() / (double)filter.TakeEntity); var pager = Pager.Build(count, filter.PageId, filter.TakeEntity); var products = await productsQuery.Paging(pager).ToListAsync(); return(filter.SetProducts(products).SetPaging(pager)); }
public async Task <FilterProductsDTO> FilterProducts(FilterProductsDTO filter) { //IQueryRable<Product> = _context.product; var productQuery = _productRepository.GetEntitiesQuery().AsQueryable(); //FilterPrice switch (filter.OrderBy) { case ProductOrderBy.PriceAsc: productQuery = productQuery.OrderBy(s => s.Price); break; case ProductOrderBy.PriceDec: productQuery = productQuery.OrderByDescending(s => s.Price); break; } //Search Title Filter if (!string.IsNullOrEmpty(filter.Title)) { productQuery = productQuery.Where(s => s.ProductName.Contains(filter.Title)); } //Price Filter productQuery = productQuery.Where(s => s.Price >= filter.StartPrice); if (filter.EndPrice != 0) { productQuery = productQuery.Where(c => c.Price <= filter.EndPrice); } if (filter.categories != null && filter.categories.Any()) { productQuery = productQuery.SelectMany(s => s.ProductSelectedCategorys.Where(f => filter.categories.Contains(f.ProductCategoryId)).Select(t => t.Product)); } //اعداد محصولات var count = (int)Math.Ceiling(productQuery.Count() / (double)filter.TakeEntity); var pager = Pager.Build(count, filter.pageId, filter.TakeEntity); var products = await productQuery.Paging(pager).ToListAsync(); //برای این ما خود فیلتر را برمیگردونیم که اگر در فیلتر محصولات استرینگی پر باشد پر باقی بماند return(filter.SetProducts(products).SetPaging(pager)); }