Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 740019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:28:02+00:00 2026-05-14T08:28:02+00:00

I’m having a case of the Mondays… I need to select blog posts based

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:28:03+00:00Added an answer on May 14, 2026 at 8:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.