tags:

views:

45

answers:

1

I'm having a case of the Mondays...

I need to select blog posts based on recent activity in the post's comments collection (a Post has a List<Comment> property and likewise, a Comment has a Post property, establishing the relationship. I don't want to show the same post twice, and I only need a subset of the entities, not all of the posts.

First thought was to grab all posts that have comments, then order those based on the most recent comment. For this to work, I'm pretty sure I'd have to limit the comments for each Post to the first/newest Comment. Last I'd simply take the top 5 (or whatever max results number I want to pass into the method).

Second thought would be to grab all of the comments, ordered by CreatedOn, and filter so there's only one Comment per Post. Then return those top (whatever) posts. This seems like the same as the first option, just going through the back door.

I've got an ugly, two query option I've got working with some LINQ on the side for filtering, but I know there's a more elegant way to do it in using the NHibernate API. Hoping to see some good ideas here.

EDIT: Here's what's working for me so far. While it works, I'm sure there's a better way to do it...

// get all comments ordered by CreatedOn date
var comments = Session.CreateCriteria(typeof(Comment)).AddOrder(new Order("CreatedOn", false)).List<Comment>();
var postIDs = (from c in comments select c.ParentPost.ID).Distinct();

// filter the comments
List<Post> posts = new List<Post>();
foreach(var postID in postIDs)
{
    var post = Get(postID); // get a Post by ID
    if(!posts.Contains(post)) // this "if" is redundant due to the Distinct filter on the PostIDs collection
    {
        posts.Add(post);
    }
}

return posts;
+2  A: 

This is using the NHibernate.LambdaExtensions syntax but should be easily transformed into a standard criteria query or hql query.

var query1 = DetachedCriteria.For<Post>()
    .CreateCriteria<Post>(x => x.Comments)
    .Add<Comment>(x => x.CreatedDate >= DateTime.Now.AddDays(-5));

query1.GetExecutableCriteria(session).List<T>();

You will also probably want to eagerly load the collection and set your limits to this applicable to what you want to avoid pulling the whole database back and the N+1 query condition of iterating over a lazily loaded list.

The first step to be solving any type of complex query with NHibernate is to start from the original SQL so really we need to start with something like

Select P.*
From Post P
Where P.PostID Exists (
    Select P1.PostID
    From Posts P1 Inner Join Comments C ON ( P1.PostID = C.PostID )
    Where P1.PostID Exists (
        Select Top 5 C1.PostID, Count(*) as PostCount 
        From Comments C1
        Group By C1.PostID
        Order By PostCount DESC
    )
    And C.CreateDate > Now - 5 days
)

This was written by hand so this is more an approximation of the query and should be able to point you in the direction of being able to solve the query. With some feedback on this I can add more on translating it to NH. Also that last outer exists might be able to be dropped in the final NH version because I believe that matrix of Post with Comments is how NH will eagerly load the entities.

Chris Marisic
That's a start, but I don't want only posts with comments from the last 5 days. I need to the top 5 results from all posts with comments ordered by their most recent comment. The target application doesn't get a lot of traffic, so if there isn't activity for x amount of time, then no posts will show up at all.
mannish
What exactly do you mean top? The posts that have the most newest comments?
Chris Marisic
Yeah, I only need 5 unique posts, but which 5 are selected is based on the age of their newest comment. So, I'm trying to grab the most recent comment for every post, then order the posts based on the date of the post's comment (assuming we're just looking at the single, newest comment for a post), and then take the top 5 posts. I know how to get the limited result set I need. I'm struggling with how to elegantly select a collection of posts based on the age of each post's newest comment.
mannish
Your solution starts to head that direction, but only looks at comments made in the last 5 days. I need the sort to apply to all comments in the direction of newest to oldest.
mannish
Just a heads up that I dropped an example of how I'm doing it now in the original question.
mannish
I went about this in a slightly different, less elegant manner as I just needed to get it done, but the input was definitely the right direction.
mannish