是什么导致了"方法'跳过'仅支持 LINQ to 实体中的排序输入?



我正试图学习如何做分页功能,并一直试图遵循微软MVC文档,但我一直得到错误

系统。NotSupportedException: 'Skip'方法只被支持用于在LINQ到实体中排序输入。方法'OrderBy'必须在方法'Skip'之前调用。

我已经尝试了一些我在网上找到的解决方案,这些都不起作用。这是我的代码

public ActionResult Index(string searchString, string currentFilter, int? page)
{

IQueryable<Production> productions = from s in db.Productions select s;
//Searches database for matching production title
if (!String.IsNullOrEmpty(searchString))
{
productions = productions.Where(s => s.Title.Contains(searchString))
.OrderBy(s => s.ProductionId); 
}
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;


int pageSize = 3;
int pageNumber = (page ?? 1);

return View(productions.ToPagedList(pageNumber, pageSize));
}

分页,跳过,…首先你必须对结果进行排序,然后才能对其进行分区。Skip()方法需要有序的结果。

IQueryable<Production> productions = from s in db.Productions select s order by DEFAULT_ORDER_KEY;

您必须将DEFAULT_ORDER_KEY更改为您想要的。

性能提示:使用默认情况下UI需要显示数据的order键,以防止重复排序和防止额外的排序成本。

相关内容

最新更新