I have a query:
topics.OrderBy(x => x.Replies.Any() ? x.Replies.OrderBy(y => y.PostedDate).Last().PostedDate : x.PostedDate);
It sorts the topics collection by last reply, or if it has no reply then by its own post date. It works great and does its job well. However, it only works if topics is of type IEnumerable<Topic>
. If I try to make it `IQueryable' I get an error saying the extension method Last() is unknown or something to that effect.
Anyone know why? For some reason getting x.Replies.Last()
within this query while topics is IQueryable throws this exception.
I would like topics to be IQueryable because I first load topics with all topics from the database, sorted by last reply. I then do .Skip() and .Take() on topics for paging. However, currently it is pulling ALL topics into memory and then doing the .Skip() and .Take() on the in-memory collection. This is unacceptable because the more topics that are added to the forum, the longer it takes to load the site, especially when the database is not running on the same computer as the website. I need the database to only return the paged rows, not all rows. I want the paging to occur at the database level.
Any ideas?
PS - I'm using LINQ to Entities with Entity Framework 4.0