gpt4 book ai didi

c# - 分页错误 :The method 'Skip' is only supported for sorted input in LINQ to Entities. 方法 'OrderBy' 必须在方法 'Skip' 之前调用

转载 作者:太空狗 更新时间:2023-10-29 21:04:49 26 4
gpt4 key购买 nike

我正在索引页上的 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 方法......但是有这个错误:

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'

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.将employee.Where代码放在switch语句之前:

2.在你的 switch 语句中添加一个 default case,并让它抛出。案例“默认”: 抛出新的 ArgumentException("error", sortBy);

  1. 使用 IOrderedQueryable 类型。

    IQueryable employee = db.Employees.AsQueryable();

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

请指出缺少的内容

最佳答案

您确实有一个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;
}

关于c# - 分页错误 :The method 'Skip' is only supported for sorted input in LINQ to Entities. 方法 'OrderBy' 必须在方法 'Skip' 之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22802729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com