ASP.NET MVC PagedList.Mvc error - The method 'OrderBy' must be called before the method 'Skip'.'

I am using ASP.NET MVC 5 with entity framework version 6. I have the PagedList.MVC nuget package installed. I get the error listed below when attempting to sort in my Index view. I'm sure it is something really simple that I am overlooking.

The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.'

Controller Code listed below:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ITRA.Models;
using PagedList;
using PagedList.Mvc;

namespace ITRA.Controllers
{
    public class SSP_ControlController : Controller
    {
        private RiskAssessmentEntities2 db = new RiskAssessmentEntities2();

        // GET: SSP_Control
        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NISTIDSortParm = String.IsNullOrEmpty(sortOrder) ? "NISTID_asc" : "";
            ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "Title_asc" : "";
            ViewBag.WorksheetStatementSortParm = String.IsNullOrEmpty(sortOrder) ? "WorksheetStatement_asc" : "";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;

            var NISTID = from NID in db.SSP_Control
                      select NID;
            var Title = from t in db.SSP_Control
                      select t;
            var WorksheetStatement = from ws in db.SSP_Control
                        select ws;
            if (!String.IsNullOrEmpty(searchString))
            {
                NISTID = NISTID.Where(NID => NID.NISTID.Contains(searchString));               
            }
            switch (sortOrder)
            {
                case "NISTID_asc":
                    NISTID = NISTID.OrderBy(NID => NID.NISTID);
                    break;
                case "Title_asc":
                    Title = Title.OrderBy(t => t.Title);
                    break;
                case "WorksheetStatement_asc":
                    WorksheetStatement = WorksheetStatement.OrderBy(ws => ws.WorksheetStatement);
                    break;
                default:
                    NISTID = NISTID.OrderBy(NID => NID.NISTID);
                    break;
            }
            int pageSize = 10;
                int pageNumber = (page ?? 1);
            return View(NISTID.ToPagedList(pageNumber, pageSize));

Open in new window


Index View code listed below:

@model PagedList.IPagedList<ITRA.Models.SSP_Control>
@using PagedList.Mvc;
@using PagedList;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "SSP Controls";
}

<h2>SSP Controls</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("Index", "SSP_Control", FormMethod.Get))
{
    <p>
        Search: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Search" />
    </p>
}
<table class="table">
    <tr>
        <th>
            @Html.ActionLink("NIST ID", "Index", new { sortOrder = ViewBag.NISTIDSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Title", "Index", new { sortOrder = ViewBag.TitleSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Worksheet Statement", "Index", new { sortOrder = ViewBag.WorksheetStatementSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Control Requirement", "Index")
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.NISTID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorksheetStatement)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ControlRequirement)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ID })
            </td>
        </tr>
    }
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

Open in new window

Intelli-SeekerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
You shouldn't be splitting up the queries (lines 37 - 40 above, var NISTID, var Title, var Worksheet).
In that case, if I want to sort by worksheet, you're creating another queryable entity list, and ordering by the column.  But then filtering and returning the original NISTID queryable.  So in that case the worksheet query has not ever been sorted.

You need to create a single query, and modify that one based on your filtering criteria.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ITRA.Models;
using PagedList;
using PagedList.Mvc;

namespace ITRA.Controllers
{
    public class SSP_ControlController : Controller
    {
        private RiskAssessmentEntities2 db = new RiskAssessmentEntities2();

        // GET: SSP_Control
        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NISTIDSortParm = String.IsNullOrEmpty(sortOrder) ? "NISTID_asc" : "";
            ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "Title_asc" : "";
            ViewBag.WorksheetStatementSortParm = String.IsNullOrEmpty(sortOrder) ? "WorksheetStatement_asc" : "";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;

            var sspQuery = from ssp in db.SSP_Control
                      select ssp;

            if (!String.IsNullOrEmpty(searchString))
            {
                sspQuery = sspQuery.Where(NID => NID.NISTID.Contains(searchString));               
            }
            switch (sortOrder)
            {
                case "NISTID_asc":
                    sspQuery = sspQuery.OrderBy(x => x.NISTID);
                    break;
                case "Title_asc":
                    sspQuery = sspQuery.OrderBy(x => x.Title);
                    break;
                case "WorksheetStatement_asc":
                    sspQuery = sspQuery.OrderBy(x => x.WorksheetStatement);
                    break;
                default:
                    sspQuery = sspQuery.OrderBy(x => x.NISTID);
                    break;
            }
            int pageSize = 10;
                int pageNumber = (page ?? 1);
            return View(sspQuery.ToPagedList(pageNumber, pageSize));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Intelli-SeekerAuthor Commented:
Thanks! You solved hours of scouring the Internet to find the right answer. I really appreciate the fast response. You solved my problem!
0
Snarf0001Commented:
No problem, glad to help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.