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'

c# entity-framework linq

Question

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

1
6
5/23/2017 12:16:56 PM

Accepted Answer

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;
    }
12
4/2/2014 6:07:41 AM

Popular Answer

Please try this code

    var employee = db.Employees.ToList()// Try to change AsQueryable to Ilist or List

    if (searchBy == "Gender")
    {
        employee = employee.Where(x => x.Gender == search || search == null).ToPagedList(Page ?? 1,3);
    }
    else
    {
        employee = employee.Where(x => x.FUllName .StartsWith(search ) || search == null).ToPagedList(Page ?? 1,3);
    }
     switch (sortBy)
    {
        case "Name desc":
            employee = employee.OrderByDescending(x => x.FUllName);
            break;

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

      return View( employee);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow