开发者

Using LINQ-to-NHibernate Sort an IQueryable of T based on a property value within T's Children

Though the project in which I'm having this issue addresses a different domain, I've found a metaphor that may help to present my question clearly:

Let's say that you had to develop a Forum web site, and it was a requirement that the default view for the site needs to display Forum Threads ordered by the threads with the most recent Forum Posts. How could this be accomplished using Linq-to-Nhibernate?

Here is an example of how those Entities might look:

namespace MvcSandbox.Models
{

public class ForumThread : Entity
{
    public virtual string Topic { get; set; }
    public virtual IList<ThreadPost> Posts { get; set; }
}

public class ThreadPost : Entity
{
    private ForumThread _thread;

    public virtual ForumThread Thread
    {
        get { return _thread; }
    }

    public virtual DateTime PostDate { get; set; }

    public virtual string PostText { get; set; }
}
}

So, given an IQuerable of ForumThread, how could Linq be used to append an OrdbyBy clause that will sort the ForumThreads based on the Max(PostDate) of their respective开发者_开发百科 Posts collection? And for performance reasons, I'm trying to find a solution that will enable the Order By to occur in the database rather than in memory.

Any input would be greatly appreciated.


Have you tried something like:

myQueryable.OrderBy(thread => thread.Posts.OrderByDescending(post => post.PostDate).FirstOrDefault())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜