public async Task OnGetAsync() { //Select Lists BodyTypes = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.BodyType, Text = s.BodyType } ) .Distinct() .ToListAsync(); Makes = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.Make, Text = s.Make } ) .Distinct() .ToListAsync(); ModelMakes = await _context.StocklistImport .Select( s => new SelectListGroup { Name = s.Make } ) .Distinct() .ToListAsync(); Models = (await _context.StocklistImport .ToListAsync()) .GroupBy(grp => new { grp.Make, grp.Model, grp.BodyType }) .Select( s => new SelectListItem { Value = s.Key.Model, Text = s.Key.Model + " (" + s.Key.BodyType + ")", Group = ModelMakes.SingleOrDefault(m => m.Name == s.Key.Make) } ) .OrderBy(s => s.Group.Name) .ThenBy(s => s.Text) .ToList(); MinPrice = null; MaxPrice = null; MinBudget = null; MaxBudget = null; }
public async Task OnGetAsync( string sortOrder, string currentFilter, string searchString, string make, string model, int?min_price, int?max_price, int?mileage, string transmission, string fuel_type, string body_type, int?pageIndex ) { //Set filter Filters = new List <Filter>(); if (!String.IsNullOrEmpty(make)) { Filters.Add(new Filter { FilterExp = "make=" + make }); } if (!String.IsNullOrEmpty(model)) { //Replace previously replaced spaces model = model.Replace("_", " "); Filters.Add(new Filter { FilterExp = "model=" + model }); } if (min_price >= 0) { Filters.Add(new Filter { FilterExp = "min_price=" + min_price }); } if (max_price >= 0) { Filters.Add(new Filter { FilterExp = "max_price=" + max_price }); } if (mileage >= 0) { Filters.Add(new Filter { FilterExp = "mileage=" + mileage }); } if (!String.IsNullOrEmpty(transmission)) { Filters.Add(new Filter { FilterExp = "transmission=" + transmission }); } if (!String.IsNullOrEmpty(fuel_type)) { Filters.Add(new Filter { FilterExp = "fuel_type=" + fuel_type }); } if (!String.IsNullOrEmpty(body_type)) { //Replace previously replaced spaces body_type = body_type.Replace("_", " "); Filters.Add(new Filter { FilterExp = "body_type=" + body_type }); } //Select Lists Makes = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.Make, Text = s.Make, Selected = s.Make == make } ) .Distinct() .ToListAsync(); ModelMakes = await _context.StocklistImport .Select( s => new SelectListGroup { Name = s.Make } ) .Distinct() .ToListAsync(); Models = _context.StocklistImport .GroupBy(grp => new { grp.Make, grp.Model }) .ToList() /*Fix for Net Core 2.1 to avoid must be reducible node error*/ .Select( s => new SelectListItem { Value = s.Key.Model, Text = s.Key.Model, Selected = s.Key.Model == model, Group = ModelMakes.SingleOrDefault(m => m.Name == s.Key.Make) } ) .Where(s => s.Group.Name.Equals(make) || make == null) .OrderBy(s => s.Group.Name) .ThenBy(s => s.Text) .ToList(); PricesFrom = Enumerable.Range(0, 26) .Select( s => new SelectListItem { Value = (s * 1000).ToString(), Text = (s * 1000).ToString("C0"), Selected = (s * 1000) == min_price } ) .Distinct() .ToList(); PricesTo = Enumerable.Range(0, 26) .Select( s => new SelectListItem { Value = (s * 1000).ToString(), Text = (s * 1000).ToString("C0"), Selected = (s * 1000) == max_price } ) .Distinct() .ToList(); Mileage = Enumerable.Range(2, 9) .Select( s => new SelectListItem { Value = (s * 10000).ToString(), Text = "Up to " + (s * 10000).ToString("N0") + " miles", Selected = (s * 10000) == mileage } ) .Distinct() .ToList(); Transmission = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.Transmission, Text = s.Transmission, Selected = s.Transmission == transmission } ) .Distinct() .ToListAsync(); FuelType = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.FuelType, Text = s.FuelType, Selected = s.FuelType == fuel_type } ) .Distinct() .ToListAsync(); BodyType = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.BodyType, Text = s.BodyType, Selected = s.BodyType == body_type } ) .Distinct() .ToListAsync(); if (searchString != null) { pageIndex = 1; } else { searchString = currentFilter; } //Add a default sort order if (sortOrder == null) { sortOrder = "Recent"; } CurrentFilterID = searchString; CurrentSortID = sortOrder; IQueryable <StocklistImport> stocklistImportIQ = from s in _context.StocklistImport select s; //Process searches if (!String.IsNullOrEmpty(make)) { stocklistImportIQ = stocklistImportIQ.Where(s => s.Make.Equals(make)); } if (!String.IsNullOrEmpty(model)) { stocklistImportIQ = stocklistImportIQ.Where(s => s.Model.Equals(model)); } if (min_price >= 0) { stocklistImportIQ = stocklistImportIQ.Where(s => s.Price >= min_price); } if (max_price >= 0) { stocklistImportIQ = stocklistImportIQ.Where(s => s.Price <= max_price); } if (mileage >= 0) { stocklistImportIQ = stocklistImportIQ.Where(s => s.Mileage <= mileage); } if (!String.IsNullOrEmpty(transmission)) { stocklistImportIQ = stocklistImportIQ.Where(s => s.Transmission.Equals(transmission)); } if (!String.IsNullOrEmpty(fuel_type)) { stocklistImportIQ = stocklistImportIQ.Where(s => s.FuelType.Equals(fuel_type)); } if (!String.IsNullOrEmpty(body_type)) { stocklistImportIQ = stocklistImportIQ.Where(s => s.BodyType.Equals(body_type)); } switch (sortOrder) { case "Latest": stocklistImportIQ = stocklistImportIQ.OrderByDescending(s => s.CreatedDate).Take(20); break; case "Recent": stocklistImportIQ = stocklistImportIQ.OrderByDescending(s => s.CreatedDate); break; case "LowMileage": stocklistImportIQ = stocklistImportIQ.OrderBy(s => s.Mileage); break; case "RecentPlates": stocklistImportIQ = stocklistImportIQ.OrderByDescending(s => s.RegCode); break; case "LowPrice": stocklistImportIQ = stocklistImportIQ.OrderBy(s => s.Price); break; default: stocklistImportIQ = stocklistImportIQ.OrderByDescending(s => s.CreatedDate); break; } //StocklistImport = await _context.StocklistImport // .Include(s => s.Photo) // .ToListAsync(); //StocklistImport = await stocklistImportIQ.AsNoTracking() // .Include(s => s.Photo) // .ToListAsync(); int pageSize = 16; StocklistImport = await PaginatedList <StocklistImport> .CreateAsync( stocklistImportIQ.AsNoTracking() .Include(s => s.Photo), pageIndex ?? 1, pageSize ); }
public async Task OnGetAsync( string sortOrder, string make, string model, string min_price, string max_price, string min_budget, string max_budget, string mileage, string transmission, string fuel_type, string body_type, int?pageIndex ) { //Select Lists BodyTypes = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.BodyType, Text = s.BodyType, Selected = s.BodyType == body_type } ) .Distinct() .ToListAsync(); Makes = await _context.StocklistImport .Select( s => new SelectListItem { Value = s.Make, Text = s.Make, Selected = s.Make == make } ) .Distinct() .ToListAsync(); ModelMakes = await _context.StocklistImport .Select( s => new SelectListGroup { Name = s.Make } ) .Distinct() .ToListAsync(); Models = (await _context.StocklistImport .ToListAsync()) .GroupBy(grp => new { grp.Make, grp.Model, grp.BodyType }) .Select( s => new SelectListItem { Value = s.Key.Model, Text = s.Key.Model + " (" + s.Key.BodyType + ")", Group = ModelMakes.SingleOrDefault(m => m.Name == s.Key.Make) } ) .OrderBy(s => s.Group.Name) .ThenBy(s => s.Text) .ToList(); MinPrice = NumberFunctions.CurrencyToInt(min_price); MaxPrice = NumberFunctions.CurrencyToInt(max_price); MinBudget = NumberFunctions.CurrencyToInt(min_budget); MaxBudget = NumberFunctions.CurrencyToInt(max_budget); //Add a default sort order if (sortOrder == null) { sortOrder = "Recent"; } CurrentSortID = sortOrder; }