My linq is something
GetPublishedArticleList().Where(x => x.Category.CatName == catName).OrderByDescending(x=>x.PublishedDate).Skip(skip).Take(last);
I am getting following exception when the above code is run
"The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
Well I want LINQ to understand that I need to order the data in descending order first and then can apply Skip and Take. (well the above code works when OrderByDescending is replaced by OrderBy)
Can someone suggest me alternative ?
This works with EF5. (.net 4.5) I cant see anything wrong with your code. Are you sure you had the method sequence right when testing ? Was the source type Iqueryable or Iqueryable ?
public virtual IQueryable<TPoco> GetSortedPageList<TSortKey>(Expression<Func<TPoco, bool>> predicate,
Expression<Func<TPoco, TSortKey>> sortBy,
bool descending,
int skipRecords, int takeRecords) {
if (!descending) {
return Context.Set<TPoco>()
.Where<TPoco> predicate)
.OrderBy(sortBy)
.Skip(skipRecords)
.Take(takeRecords);
}
return
Context.Set<TPoco>()
.Where<TPoco>(predicate)
.OrderByDescending(sortBy)
.Skip(skipRecords)
.Take(takeRecords);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With