尝试使用 IQueryable 自定义 OrderBy - “跳过”方法错误

发布于 2024-10-18 01:27:33 字数 675 浏览 4 评论 0原文

我的代码的第一部分获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-10-25 01:27:33

您的意思是

switch (sort)
{
case 1:
    issues = issues.OrderBy(x => x.Customer);
    break;
case 2:
    issues = issues.OrderBy(x => x.Description);
    break;
case 3:
    issues = issues.OrderBy(x => x.CreatedBy);
    break;
default:
    issues = issues.OrderBy(x => x.DueDateTime);
    break;
}

您可能需要更改 issues 的类型,或将其设置为新变量,因为它返回 IOrderedQueryable

Did you perhaps mean

switch (sort)
{
case 1:
    issues = issues.OrderBy(x => x.Customer);
    break;
case 2:
    issues = issues.OrderBy(x => x.Description);
    break;
case 3:
    issues = issues.OrderBy(x => x.CreatedBy);
    break;
default:
    issues = issues.OrderBy(x => x.DueDateTime);
    break;
}

You may need to change the type of issues, or set it to a new variable, since it returns IOrderedQueryable<T>

夏末的微笑 2024-10-25 01:27:33

您还没有向我们展示您的所有代码,但有一个问题是您期望 issues.OrderBy(..)改变由 <代码>问题变量。但它实际上并没有做到这一点;它返回一个新的IOrderedQueryable,它表示原始可查询的有序版本。

由于您的排序操作实际上并未触及您的 issues 变量所指的可查询;对它调用 Skip 会导致您收到错误,这是有道理的,因为它实际上尚未排序。

您可能想做:

issues = issues.OrderBy(...);

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 the issues variable. But it doesn't actually do that; it returns a new IOrderedQueryable 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 calling Skip on it results in the error that you're getting, because it hasn't actually been sorted.

You probably want to do:

issues = issues.OrderBy(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文