public SortLampsViewModel(LampsSortState sortOrder) { LampNameAscSort = sortOrder == LampsSortState.LampNameAsc ? LampsSortState.LampNameIdDesc : LampsSortState.LampNameAsc; LampTypeAscSort = sortOrder == LampsSortState.LampTypeAsc ? LampsSortState.LampTypeDesc : LampsSortState.LampTypeAsc; LifeTimeAscSort = sortOrder == LampsSortState.LifeTimeAsc ? LampsSortState.LifeTimeDesc : LampsSortState.LifeTimeAsc; PowerAscSort = sortOrder == LampsSortState.PowerAsc ? LampsSortState.PowerDesc : LampsSortState.PowerAsc; Current = sortOrder; }
// GET: Lamps public IActionResult Index(string lampName, string lampType, int?lifetime, int?Power, string streetName, string sectionName, int page = 1, LampsSortState sortOrder = LampsSortState.LampNameAsc) { int pageSize = 10; IQueryable <Lamps> source = _context.Lamps; IQueryable <StreetLightings> sourse = _context.StreetLightings.Include(x => x.SectionId); if (lampName != null) { source = source.Where(x => x.LampName.Contains(lampName)); } if (lampType != null) { source = source.Where(x => x.LampType.Contains(lampType)); } if (lifetime != 0 && lifetime != null) { source = source.Where(x => x.LifeTime == lifetime); } if (Power != 0 && Power != null) { source = source.Where(x => x.Power == Power); } if (streetName != null) { List <Streets> streets = _context.Streets.Where(x => x.StreetName == streetName).ToList(); List <Sections> sections = new List <Sections>(); foreach (var item in streets) { sections.AddRange(_context.Sections.Where(x => x.StreetId == item.StreetId)); } List <Lamps> lamps = new List <Lamps>(); foreach (var item in sections) { lamps.AddRange(_context.StreetLightings.Where(x => x.SectionId == item.SectionId).Select(x => x.Lamp)); } source = lamps.AsQueryable(); } if (sectionName != null) { List <Sections> sections = _context.Sections.Where(x => x.SectionName == sectionName).ToList(); List <Lamps> lamps = new List <Lamps>(); foreach (var item in sections) { lamps.AddRange(_context.StreetLightings.Where(x => x.SectionId == item.SectionId).Select(x => x.Lamp)); } source = lamps.AsQueryable(); } switch (sortOrder) { case LampsSortState.LampNameAsc: source = source.OrderBy(x => x.LampName); break; case LampsSortState.LampNameIdDesc: source = source.OrderByDescending(x => x.LampName); break; case LampsSortState.LampTypeAsc: source = source.OrderBy(x => x.LampType); break; case LampsSortState.LampTypeDesc: source = source.OrderByDescending(x => x.LampType); break; case LampsSortState.LifeTimeAsc: source = source.OrderBy(x => x.LifeTime); break; case LampsSortState.LifeTimeDesc: source = source.OrderByDescending(x => x.LifeTime); break; case LampsSortState.PowerAsc: source = source.OrderBy(x => x.Power); break; case LampsSortState.PowerDesc: source = source.OrderByDescending(x => x.Power); break; default: source = source.OrderBy(x => x.LampName); break; } var count = source.Count(); IEnumerable <Lamps> items = source.Skip((page - 1) * pageSize).Take(pageSize); //var items = source.ToList(); PageViewModel pageView = new PageViewModel(count, page, pageSize); LampsViewModel ivm = new LampsViewModel { PageViewModel = pageView, SortViewModel = new SortLampsViewModel(sortOrder), FilterViewModel = new FilterLampsViewModel(lampName, lampType, lifetime, Power, streetName, sectionName), Lamps = items, Users = _context.User }; return(View(ivm)); }