public SortStreetLightingsViewModel(StreetLightingsSortState sortOrder) { CountLanternAscSort = sortOrder == StreetLightingsSortState.CountLanternAsc ? StreetLightingsSortState.CountLanternDesc : StreetLightingsSortState.CountLanternAsc; FailureAscSort = sortOrder == StreetLightingsSortState.FailureAsc ? StreetLightingsSortState.FailureDesc : StreetLightingsSortState.FailureAsc; LampNameAscSort = sortOrder == StreetLightingsSortState.LampNameAsc ? StreetLightingsSortState.LampNameDesc : StreetLightingsSortState.LampNameAsc; LanternNameAscSort = sortOrder == StreetLightingsSortState.LanternNameAsc ? StreetLightingsSortState.LanternNameDesc : StreetLightingsSortState.LanternNameAsc; SectionNameAscSort = sortOrder == StreetLightingsSortState.SectionNameAsc ? StreetLightingsSortState.SectionNameDesc : StreetLightingsSortState.SectionNameAsc; Current = sortOrder; }
// GET: StreetLightings public IActionResult Index(int?countLantern, DateTime?failure, string lampName, string lanternName, string sectionName, string streetName, int page = 1, StreetLightingsSortState sortOrder = StreetLightingsSortState.CountLanternAsc) { int pageSize = 10; IQueryable <StreetLightings> source = _context.StreetLightings.Include(x => x.Lamp).Include(x => x.Lantern).Include(x => x.Section); if (countLantern != 0 && countLantern != null) { source = source.Where(x => x.CountLantern == countLantern); } if (failure != null) { source = source.Where(x => x.Failure.Equals(failure)); } if (lampName != null) { source = source.Where(x => x.Lamp.LampName.Contains(lampName)); } if (lanternName != null) { source = source.Where(x => x.Lantern.LanternName.Contains(lanternName)); } if (sectionName != null) { source = source.Where(x => x.Section.SectionName.Contains(sectionName)); } 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 <StreetLightings> streetLightings = new List <StreetLightings>(); foreach (var item in sections) { streetLightings.AddRange(_context.StreetLightings.Where(x => x.SectionId == item.SectionId)); } source = streetLightings.AsQueryable(); } switch (sortOrder) { case StreetLightingsSortState.CountLanternAsc: source = source.OrderBy(x => x.CountLantern); break; case StreetLightingsSortState.CountLanternDesc: source = source.OrderByDescending(x => x.CountLantern); break; case StreetLightingsSortState.FailureAsc: source = source.OrderBy(x => x.Failure); break; case StreetLightingsSortState.FailureDesc: source = source.OrderByDescending(x => x.Failure); break; case StreetLightingsSortState.LampNameAsc: source = source.OrderBy(x => x.Lamp.LampName); break; case StreetLightingsSortState.LampNameDesc: source = source.OrderByDescending(x => x.Lamp.LampName); break; case StreetLightingsSortState.LanternNameAsc: source = source.OrderBy(x => x.Lantern.LanternName); break; case StreetLightingsSortState.LanternNameDesc: source = source.OrderByDescending(x => x.Lantern.LanternName); break; case StreetLightingsSortState.SectionNameAsc: source = source.OrderBy(x => x.Section.SectionName); break; case StreetLightingsSortState.SectionNameDesc: source = source.OrderByDescending(x => x.Section.SectionName); break; default: source = source.OrderBy(x => x.CountLantern); break; } var count = source.Count(); IEnumerable <StreetLightings> items = source.Skip((page - 1) * pageSize).Take(pageSize); //var items = source.ToList(); PageViewModel pageView = new PageViewModel(count, page, pageSize); StreetLightingsViewModel ivm = new StreetLightingsViewModel { PageViewModel = pageView, SortViewModel = new SortStreetLightingsViewModel(sortOrder), FilterViewModel = new FilterStreetLightingsViewModel(countLantern, failure, lampName, lanternName, sectionName, streetName), StreetLightings = items, Users = _context.User }; return(View(ivm)); }