Reputation:
I want to use Skip and Take for my paging, but I also want to use orderby
Without the Skip and Take, with LinqPad I can do the following and this works with orderby
var query = from m in Orders
orderby m.ID descending
select m;
query.Dump();
However, if I keep the orderby in, then this fails
query = query.Skip(1 + 10).Take(10);
query.Dump();
I remove the orderby and it works, but I want the orderby ....
var query = from m in Orders
select m;
query = query.Skip(1 + 10).Take(10);
query.Dump();
Upvotes: 9
Views: 18711
Reputation: 267
for more. if you use it or not you can sort your query without ascending or descending style. you can use below code.
var comments = (from c in ESO_tblComments
.Where(d => d.TblTipsId == 10312)
select c).OrderBy(xx=>1==1).Skip(1);
Upvotes: 1
Reputation: 20401
The selected answer is correct, however If you are not binding to a model with of Type Order, then this would work
MarcinJuraszek is correct.
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Order>' to 'System.Linq.IOrderedEnumerable<Order>'. An explicit conversion exists (are you missing a cast?)
You will get that error the way you are doing it
HOWEVER to NOT have to use IEnumerable<Order>
...
var query = (from m in Orders
orderby m.ID descending
select m).Skip(10).Take(10);
query.Dump();
I had the same issue in LINQPAD when I knew that I wanted to skip over the first record
So for me :
var comments = (from c in ESO_tblComments
.Where(d => d.TblTipsId == 10312)
orderby c.Id ascending
select c).Skip(1);
Upvotes: 1
Reputation: 125630
Error occurs, because you use var
and compiler infer query
type as IOrderedEnumerable<T>
after your first query, and you're trying to assign IEnumerable<T>
to it with Skip
/Take
calls:
Cannot implicitly convert type '
System.Collections.Generic.IEnumerable<Order>
' to 'System.Linq.IOrderedEnumerable<Order>
'. An explicit conversion exists (are you missing a cast?)
If you type query
as IEnumerable<T>
at the beginning everything will work just fine
IEnumerable<Order> query = from m in Orders
orderby m.ID descending
select m;
query = query.Skip(10).Take(10);
Upvotes: 20