It is not possible to implicitly convert the type 'System.Linq.IQueryable <>' to 'System.Linq.IOrderedQueryable <>'

Possible duplicate:
LINQ2SQL: unable to convert IQueryable <> to IOrderedQueryable error

I have this error in the searchString of my controller.

here are my codes:

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

I use these codes for my search in my Index. Am I saying something wrong here or something like that? I already tried searching, but did not find a clear answer. Thanks in advance for everyone who could help me ..

+6
source share
3 answers

The most likely reason is the use of an imp var declaration for posts in a request with OrderBy . If you replace it with

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

the error should disappear.

+16
source

Your posts variable is of type IOrderedQueryable<Post> , but you assign it an IQueryable<Post> . I see two ways to solve this problem: either change the type of the variable, or IQueryable<Post> , or sort the query as follows:

 posts = posts.Where(post => post.Body == searchString) .OrderBy(post => post.Date); 
+5
source

messages is an IOrderedQueryable and a call to where the IQueeryable is returned. Why don't you explicitly declare the variable and see if it fixes the problem.

0
source

All Articles