Using NHibernate to select entities based on activity of children entities
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 comme开发者_开发百科nts 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;
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.
精彩评论