Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am Doing Paging in MVC on Index Page ..on this Line I got error

return View( employee.ToPagedList(Page ?? 1,3));

here is Index Method

 public ActionResult Index(string searchBy, string search, int? Page, string sortBy)
    {
        ViewBag.SortNameParameter = string.IsNullOrEmpty(sortBy) ? "Name desc"
            : "";

        ViewBag.SortGenderParameter = string.IsNullOrEmpty(sortBy) ? "Gender desc"
            : "Gender";

        var employee = db.Employees.AsQueryable();

        if (searchBy == "Gender")
        {
            employee = employee.Where(x => x.Gender == search || search == null);
        }
        else
        {
            employee = employee.Where(x => x.FUllName .StartsWith(search ) || search == null);
        }

        switch (sortBy)
        {
            case "Name desc":
                employee = employee.OrderByDescending(x => x.FUllName);
                break;

            case "Default":
                employee = employee.OrderBy(x => x.FUllName);
                break;
        }

      return View( employee.ToPagedList(Page ?? 1,3));
    }

I am not using Skip method ... But there is this error :

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

I read Similar Posts

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

How to solve "The method 'Skip' is only supported for sorted input in LINQ to Entities."

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

ASP.NET MVC 3 PagedList. The method 'Skip' is only supported for sorted input in LINQ to Entities.

and did following changes ...

1.Put employee.Where code before the switch statement:

2.Add a default case in your switch statement, and make it throw. case "Default": throw new ArgumentException("error", sortBy);

  1. Use the type IOrderedQueryable.

    IQueryable employee = db.Employees.AsQueryable();

this doesnot solve problem ..
mostly they use to have Skip method .. But i Dont have ... and Other posts query are complex one ..

Please suggest whats missing

like image 867
user3487944 Avatar asked Apr 02 '14 05:04

user3487944


1 Answers

You do have a Skip method.

The PagedList added it for you. Check the code out. That's how paging works, Take and Skip.

Also I think your case statement was meant to be

    switch (sortBy)
    {
        case "Name desc":
            employee = employee.OrderByDescending(x => x.FUllName);
            break;

        default: // Not: case "Default"
            employee = employee.OrderBy(x => x.FUllName);
            break;
    }
like image 105
Meligy Avatar answered Sep 19 '22 12:09

Meligy