public IQueryable <Product> GetSortedQuery(int startIndex, int length, Product.SortType sortType, string searchName, int minPrice, int maxPrice) { int count = Products.Count(); if (count == 0) { return(Products); } if (startIndex >= count || startIndex < 0) { throw new IndexOutOfRangeException(); } if (count - startIndex < length) { length = count - startIndex; } IQueryable <Product> products; #region GetSortParams Expression <Func <Product, object> > sortSelector; switch (sortType) { case Product.SortType.name: case Product.SortType.nameDesc: sortSelector = c => c._Name; break; case Product.SortType.price: case Product.SortType.priceDesc: sortSelector = c => c._Price; break; default: throw new ArgumentOutOfRangeException(); } #endregion if (searchName == null) { searchName = ""; } Expression <Func <Product, bool> > filterSelector = x => x._Name.StartsWith(searchName) && (x._Price >= minPrice) && (x._Price <= maxPrice);// if ((((int)sortType) % 2) != 0) { products = Products.Where(filterSelector).OrderByDescending(sortSelector).Skip(startIndex).Take(length);// } else { products = Products.Where(filterSelector).OrderBy(sortSelector).Skip(startIndex).Take(length);// } List <Product> lili = products.ToList(); return(products); }
internal static ProductPageModel GetPageModel(int currentPageNum, AppDatabaseContext ctx, Product.SortType sortType, string searchName, int minPrice, int maxPrice) { var pageCount = ctx.Products.Count() / MAX_PAGE_LENGTH; if (ctx.Products.Count() % MAX_PAGE_LENGTH != 0) { pageCount++; } if (pageCount <= currentPageNum) { return(EMPTY_MODEL); } ProductPageModel model = new ProductPageModel { _sortType = sortType.ToString(), _pageCount = pageCount, _currentPageNum = currentPageNum, _entries = ctx.GetSortedQuery(currentPageNum * MAX_PAGE_LENGTH, MAX_PAGE_LENGTH, sortType, searchName, minPrice, maxPrice) }; return(model); }