I have an entity in Entity Framework 4 called Topic. Topic has a navigation property to a collection of Reply entities.
I am trying to craft a query that obtains the most popular topic based on the number of replies.
entityContainer.Topics.Single(x => x./* has the most replies of any topic */);
I’m not sure how to craft a query to accomplish this.
This might accomplish what you’re looking to do.
I imagine this is going to have terrible perf, tho.
A better option might be to denormalize your data (slightly) and add a reply count column to the
Topictable. Then you can index that column, and do a simple sort and retrieve for row. This will avoid a whole table scan ofTopicsand a count (at query time) on everyRepliesentry.The trade-off will be that you have to be careful to ensure that the
Repliescount always gets updated, or live with results that aren’t always completely up to date and do a background job to rebuild these values.