1

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

CatDadCode
  • 58,507
  • 61
  • 212
  • 318

4 Answers4

3

Have you tried topics.OrderBy(x => x.Replies.Any() ? x.Replies.Max(y => y.PostedDate) : x.PostedDate);?

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • This was exactly what I needed. It translated nicely into a store expression to be used with IQueryable and sent to EF for low-level querying. Woo hoo! – CatDadCode Jan 17 '11 at 21:16
1

IEnumerable : LINQ to Object and LINQ to XML.

IQueryable : LINQ to SQL

Najeebullah Shah
  • 4,164
  • 4
  • 35
  • 49
1

Is you context to the database still active when you try to run the code you posted? If you (or .NET) have disposed the database context you will no longer be able to access the data in the IQueryable object because the connection is lost.

/Viktor

Viktor
  • 476
  • 3
  • 16
  • This query is part of an extension method called OrderByLastReply. Would the context be lost there? If so, is it possible to preserve the context in an extension method? – CatDadCode Jan 16 '11 at 07:48
  • That depends very much on whet your code looks like. If you have a object of the type IQueryable it will not be executed until it is needed so if you have a using statement with your database context you have to make sure that the query is executed within this statement and if you just return the IQueryable object this will not have happen and you will get a exception when you try to execute the query. It can be done in many ways but the easy way is to just call .ToList() on the object and return the list. My guess for you is that you have to move your paging code closer to the database query. – Viktor Jan 16 '11 at 07:59
  • Your are correct. I am using the repository pattern. The repository is returning IQueryable and the controller is doing the paging. I will move the paging into a new method in the repository that will do the paging and just return IEnumerable so I don't have to worry about query stuff in the controller. I will let you know how it turns out. – CatDadCode Jan 16 '11 at 16:26
0

I think EF is unable to translate your query correctly, it happens. Because it is not necessary that every lamda will convert successfully into esql, it has its own limitations. I think it is very complicated SQL query for what you are looking for.

I will suggest you can create a new DateTime field in your topics called "LastUpdate", which will be defaulted to Topic's PostDate when it will be created. Second when you add a new reply, you make sure you update Topic's LastUpdate as well.

By doing this, you also make your query very simple, and you avoid joining unnecessary while creating your query. You don't need to look for PostDate of replies table with any join.

I do this kind of Caching (which is called denormalization in database design terms), this is not perfect in terms of design but by performance and coding, its much easier and simpler.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • Yeah, I've been hoping to avoid this. For one, I need the last reply date AND the last reply author. Also, if the last reply gets deleted then I want the sorting to fix itself and drop the topic back where it was. With the extra field the topic would stay bumped with no new replies. – CatDadCode Jan 16 '11 at 16:23
  • Yes I agree, but if you think in terms of performance, you read row of the table 99 times and you modify only once, with EF it shouldnt be that difficult, when you delete your reply, you will have to add extra logic to empty reply's topic details. I have noticed increased performance with caching, believe me whether we like it or not, even inside CPU you have cache, IE has temp files, everywhere cache works better. – Akash Kava Jan 17 '11 at 09:16