无法转换的IQueryable<>到IOrderedQueryable错误 [英] Cannot convert IQueryable<> to IOrderedQueryable error
问题描述
我有以下的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<>到IOrderedQueryable错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!