尝试使用 IQueryable 自定义 OrderBy - “跳过”方法错误
我的代码的第一部分获取 IQueryable 数据结果:
var issues = repository.GetAllIssues().Where(i =>
i.IssueNotificationRecipients.Any(r => r.Status == "Open"));
然后我确定用户请求的排序顺序,并添加它:
switch (sort)
{
case 1:
issues.OrderBy(x => x.Customer);
break;
case 2:
issues.OrderBy(x => x.Description);
break;
case 3:
issues.OrderBy(x => x.CreatedBy);
break;
default:
issues.OrderBy(x => x.DueDateTime);
break;
}
这会引发错误:
仅支持“跳过”方法 用于 LINQ to Entities 中的排序输入。 必须调用“OrderBy”方法 在方法“Skip”之前
那么如何动态添加 OrderBy 以响应用户的输入呢?
The first part of my code gets the IQueryable data results:
var issues = repository.GetAllIssues().Where(i =>
i.IssueNotificationRecipients.Any(r => r.Status == "Open"));
Then I determine which sort order the user has requested, and add it:
switch (sort)
{
case 1:
issues.OrderBy(x => x.Customer);
break;
case 2:
issues.OrderBy(x => x.Description);
break;
case 3:
issues.OrderBy(x => x.CreatedBy);
break;
default:
issues.OrderBy(x => x.DueDateTime);
break;
}
This throws the error:
The method 'Skip' is only supported
for sorted input in LINQ to Entities.
The method 'OrderBy' must be called
before the method 'Skip'
So how can I add the OrderBy dynamically, in response to my user's input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的意思是
您可能需要更改
issues
的类型,或将其设置为新变量,因为它返回IOrderedQueryable
Did you perhaps mean
You may need to change the type of
issues
, or set it to a new variable, since it returnsIOrderedQueryable<T>
您还没有向我们展示您的所有代码,但有一个问题是您期望
issues.OrderBy(..)
来改变由 <代码>问题变量。但它实际上并没有做到这一点;它返回一个新的IOrderedQueryable
,它表示原始可查询的有序版本。由于您的排序操作实际上并未触及您的
issues
变量所指的可查询;对它调用Skip
会导致您收到错误,这是有道理的,因为它实际上尚未排序。您可能想做:
You haven't shown us all of your code, but one problem is that you're expecting
issues.OrderBy(..)
to mutate the queryable referred to by theissues
variable. But it doesn't actually do that; it returns a newIOrderedQueryable
that represents an ordered version of the original queryable.Since your ordering operations aren't actually touching the queryable referred to be your
issues
variable; it makes sense that callingSkip
on it results in the error that you're getting, because it hasn't actually been sorted.You probably want to do: