private ProductGridViewModel loadProductGridViewModel(int?categoryId, int page, int pageSize, string keywords = null, bool?featured = null, int?maxCount = null, int[] optionIds = null, int?excludeProductId = null) { IQueryable <Product> products = productFinder.Find(categoryId, keywords: keywords, featured: featured, optionIds: optionIds); if (excludeProductId.HasValue) { products = products.Where(p => p.Id != excludeProductId); } if (maxCount.HasValue) { products = products.Take(maxCount.Value); } var count = products.Count(); if (count == 0) { products = productFinder.Find(categoryId, keywords: keywords, includeChildCategories: true, featured: featured, optionIds: optionIds); if (excludeProductId.HasValue) { products = products.Where(p => p.Id != excludeProductId); } if (maxCount.HasValue) { products = products.Take(maxCount.Value); } count = products.Count(); } var model = new ProductGridViewModel(); model.Page = page; model.TotalProducts = count; model.TotalPages = ((model.TotalProducts - 1) / pageSize) + 1; model.Products = Mapper.Map <ProductViewModel[]>(products.GetPage(page, pageSize)); foreach (var productViewModel in model.Products) { productViewModel.Url = Url.Action("Details", new { id = productViewModel.Id, slug = productViewModel.Name.ToSlug() }); productViewModel.PrimaryPhotoId = uploadService.FindPrimaryIdByProduct(productViewModel.Id); } model.CategoryId = categoryId; model.Keywords = keywords; model.Featured = featured; return(model); }