无法转换 IQueryable<> IOrderedQueryable 错误

发布于 2024-08-10 23:39:40 字数 550 浏览 5 评论 0 原文

我有以下 LINQ 代码:

    var posts = (from p in db.Posts
         .Include("Site")
         .Include("PostStatus")
        where p.Public == false
        orderby p.PublicationTime 
        select p);

        if (!chkShowIgnored.Checked) {
            posts = posts.Where(p => p.PostStatus.Id != 90);
        }

最后一行(额外的位置)给了我错误:

无法将类型“System.Linq.IQueryable”隐式转换为“System.Linq.IOrderedQueryable”。

我不确定这意味着什么...
为什么我会收到此错误?
一旦我将“orderby”子句添加到查询中,它就出现了,在此之前它编译得很好,所以我对正在发生的事情有一种预感,但我不能完全了解它。

I have the following LINQ code:

    var posts = (from p in db.Posts
         .Include("Site")
         .Include("PostStatus")
        where p.Public == false
        orderby p.PublicationTime 
        select p);

        if (!chkShowIgnored.Checked) {
            posts = posts.Where(p => p.PostStatus.Id != 90);
        }

That last line (the extra where) is giving me the error:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IOrderedQueryable'.

I'm not sure what this means...
Why am I getting this error?
It appeared once I added the "orderby" clause to the query, before that it compiled fine, so I have kind of a hunch of what is going on, but I can't quite put my finger into it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

北方的巷 2024-08-17 23:39:41

尝试将 posts 专门声明为 IQueryable 而不是 var (这将获取 IOrderedQueryable (它仍然会排序)

或者,重新构造它,以便我们在最后排序,允许我们(可选)在中间引入where

var posts = from p in db.Posts
             .Include("Site")
             .Include("PostStatus")
            where p.Public == false
            select p);

if (!chkShowIgnored.Checked) {
    posts = posts.Where(p => p.PostStatus.Id != 90);
}
var finalQuery = posts.OrderBy(p => p.PublicationTime);

:(显然,我们看一下finalQuery

它出错的原因是目前你(本质上)有:

IOrderedQueryable<Post> posts = {snip};
...
posts = {something (Where) that returns IQueryable<Post>}

Try declaring posts specifically as IQueryable<Post> rather than var (which will pick up the IOrderedQueryable<Post> (it will still be ordered).

Alternatively, re-structure it so we order at the end, allowing us to (optionally) bring in the where in the middle:

var posts = from p in db.Posts
             .Include("Site")
             .Include("PostStatus")
            where p.Public == false
            select p);

if (!chkShowIgnored.Checked) {
    posts = posts.Where(p => p.PostStatus.Id != 90);
}
var finalQuery = posts.OrderBy(p => p.PublicationTime);

(obviously, we look at finalQuery)

The reason it is erroring is that currently you have (essentially):

IOrderedQueryable<Post> posts = {snip};
...
posts = {something (Where) that returns IQueryable<Post>}
扛起拖把扫天下 2024-08-17 23:39:41

lambda 表达式的结果是 IQueryable 类型。它不允许扩展方法Where,因此要首先使用它,您必须将其转换为例如列表。

你可以使用以下方法来做到这一点

posts = posts.ToList().Where(p => p.PostStatus.Id != 90);

The result of the lambda expressionis of type IQueryable. It doesn't allows the extension method Where, so to use it first you must convert this to, for example, a list.

You could do this using

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