c# .net

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

The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'. , someone asked me to explain?

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

I got this above error while running the asp.net mvc application.I want to generate paging control for grid. So I had installed pagedList Library in the application. I used the following function,

      public ActionResult Index(int? page)
        {
            int maxRows = 5;
            int pageNumber = (page ?? 1);
            return View(db.Staffs.ToPagedList(pageNumber, maxRows));
        } 

Solution:

You should convert IList or IEnumerable into IQueryable in asp.net MVC Application like below. 

  public ActionResult Index(int? page)
        {
            int maxRows = 5;
            IQueryable<Staff> staffs = (from staff in db.Staffs
                                        select staff)
                        .OrderBy(student =>student.StaffId);
            int pageNumber = (page ?? 1);
            return View(staffs.ToPagedList(pageNumber, maxRows));        } 

Post your comments / questions