Пример #1
0
        // GET: Lanterns
        public IActionResult Index(string lanternName, string lanternType, string streetName, string sectionName, int page = 1, LanternsSortState sortOrder = LanternsSortState.LanternNameAsc)
        {
            int pageSize = 10;
            IQueryable <Lanterns> source = _context.Lanterns;

            if (lanternName != null)
            {
                source = source.Where(x => x.LanternName.Contains(lanternName));
            }
            if (lanternType != null)
            {
                source = source.Where(x => x.LanternName.Contains(lanternType));
            }
            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 <Lanterns> lanterns = new List <Lanterns>();
                foreach (var item in sections)
                {
                    lanterns.AddRange(_context.StreetLightings.Where(x => x.SectionId == item.SectionId).Select(x => x.Lantern));
                }
                source = lanterns.AsQueryable();
            }
            if (sectionName != null)
            {
                List <Sections> sections = _context.Sections.Where(x => x.SectionName == sectionName).ToList();
                List <Lanterns> lanterns = new List <Lanterns>();
                foreach (var item in sections)
                {
                    lanterns.AddRange(_context.StreetLightings.Where(x => x.SectionId == item.SectionId).Select(x => x.Lantern));
                }
                source = lanterns.AsQueryable();
            }

            switch (sortOrder)
            {
            case LanternsSortState.LanternNameAsc:
                source = source.OrderBy(x => x.LanternName);
                break;

            case LanternsSortState.LanternNameDesc:
                source = source.OrderByDescending(x => x.LanternName);
                break;

            case LanternsSortState.LanternTypeAsc:
                source = source.OrderBy(x => x.LanternType);
                break;

            case LanternsSortState.LanternTypeDesc:
                source = source.OrderByDescending(x => x.LanternType);
                break;

            default:
                source = source.OrderBy(x => x.LanternName);
                break;
            }



            var               count    = source.Count();
            var               items    = source.Skip((page - 1) * pageSize).Take(pageSize);
            PageViewModel     pageView = new PageViewModel(count, page, pageSize);
            LanternsViewModel ivm      = new LanternsViewModel
            {
                PageViewModel   = pageView,
                SortViewModel   = new SortLanternsViewModel(sortOrder),
                FilterViewModel = new FilterLanternsViewModel(lanternName, lanternType, streetName, sectionName),
                Lanterns        = items,
                Users           = _context.User
            };

            return(View(ivm));
        }
 public SortLanternsViewModel(LanternsSortState sortOrder)
 {
     LanternNameAscSort = sortOrder == LanternsSortState.LanternNameAsc ? LanternsSortState.LanternNameDesc : LanternsSortState.LanternNameAsc;
     LanternTypeAscSort = sortOrder == LanternsSortState.LanternTypeAsc ? LanternsSortState.LanternTypeDesc : LanternsSortState.LanternTypeAsc;
     Current            = sortOrder;
 }