7

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

2 Answers 2

12

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;
    }
1
  • 1
    yes .. this is the answer ...default.. Not: case "Default" ...Thanks Apr 3, 2014 at 13:26
0

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);
3
  • then Type conversion error comes...Error 1 Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<_SearchDemo.Models.Employee>' to 'PagedList.IPagedList<_SearchDemo.Models.Employee>'. An explicit conversion exists (are you missing a cast?) ... Apr 2, 2014 at 6:32
  • I tried to typecast employee to IPagedList<Employee> type and var type .. but that doesnt solve issue Apr 2, 2014 at 6:33
  • Then change var employee = db.Employees.AsQueryable(); to IList<Employee> employees
    – Lineesh
    Apr 2, 2014 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.