Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type 'System.Linq.IQueryable<>' to 'System.Linq.IOrderedQueryable<>' [duplicate]

Tags:

c#

Possible Duplicate:
LINQ2SQL: Cannot convert IQueryable<> to IOrderedQueryable error

I having this error in the searchString of my controller..

here is my codes:

if (!String.IsNullOrEmpty(searchString))
{ 
    posts = posts.Where(post => post.Body == searchString);
}

I'm using this codes for my search in my Index. Did I declare something wrong here or something? I already tried googling but didn't find a clear answer. Thanks in advance for anyone who could help me..

like image 733
bot Avatar asked Jan 09 '13 04:01

bot


2 Answers

The most likely cause is that you used an implicit var declaration for posts in a query that has an OrderBy. If you replace it with

IQueryable<Post> posts = someSource.Where(cond).OrderBy(/*the culprit*/);

the error should go away.

like image 180
Sergey Kalinichenko Avatar answered Oct 29 '22 15:10

Sergey Kalinichenko


Your posts variable seems to be of type IOrderedQueryable<Post> but you are assigning it a IQueryable<Post>. I see 2 ways to resolve this, either modify your variable type to be IQueryable<Post> or sort your query like this:

posts = posts.Where(post => post.Body == searchString)
    .OrderBy(post => post.Date);
like image 36
Marcel Gosselin Avatar answered Oct 29 '22 15:10

Marcel Gosselin