无法转换的IQueryable<>到IOrderedQueryable错误 [英] Cannot convert IQueryable<> to IOrderedQueryable error

查看:1368
本文介绍了无法转换的IQueryable<>到IOrderedQueryable错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的LINQ代码:

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:

无法隐式转换类型'System.Linq.IQueryable'到'System.Linq.IOrderedQueryable。

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.

推荐答案

尝试宣告帖子专门为的IQueryable<邮政> ,而不是 VAR (将拿起 IOrderedQueryable<邮政方式> (它仍将订购)

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);



(显然,我们来看看 finalQuery

有示数的原因是,目前你有(本质):

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

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

这篇关于无法转换的IQueryable&LT;&GT;到IOrderedQueryable错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆