分页错误:仅在LINQ to Entities中对已排序的输入支持方法'Skip'。必须在方法'Skip'之前调用'OrderBy'方法

时间:2014-04-02 05:59:50

标签: c# linq entity-framework

我在索引页面上的MVC中进行寻呼..在这行我得到了错误

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

这是索引方法

 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));
    }

我没有使用Skip方法......但是有这个错误:

  

方法'Skip'仅支持LINQ to中的排序输入   实体。必须在方法之前调用方法'OrderBy'   '跳过'。

我读过类似帖子

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.

并做了以下更改......

1.Put employee.wif语句之前的代码:

2.在switch语句中添加一个默认大小写,并使其抛出。 案例“默认”:                     抛出新的ArgumentException(“error”,sortBy);

  1. 使用IOrderedQueryable类型。

    IQueryable employee = db.Employees.AsQueryable();

  2. 这不能解决问题。
    他们大多使用Skip方法..但我没有...而其他帖子查询都很复杂..

    请建议丢失什么

2 个答案:

答案 0 :(得分:12)

你有一个Skip方法。

PagedList为您添加了它。检查代码。这就是分页的工作方式,TakeSkip

此外,我认为你的案例陈述是

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

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

答案 1 :(得分: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);