IEnumerable vs IQueryable?

Have you tried topics. OrderBy(x => x.Replies.Any()? X.Replies.

Max(y => y. PostedDate) : x. PostedDate)?

Have you tried topics. OrderBy(x => x.Replies.Any()? X.Replies.

Max(y => y. PostedDate) : x. PostedDate);?

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!

– Alex Ford Jan 17 '11 at 21:16.

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.

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?

– Alex Ford Jan 16 '11 at 7: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 7: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. – Alex Ford Jan 16 '11 at 16:26.

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.

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. – Alex Ford 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 9:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions