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 6343923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:32:20+00:00 2026-05-24T20:32:20+00:00

Is there a way to achieve SQL like this with NHibernate ICriteria or QueryOver?

  • 0

Is there a way to achieve SQL like this with NHibernate ICriteria or QueryOver?

select *
  from [BlogPost] b
  inner join (select blogpost_id, count(*) matchCount
                from [Tag]
                where name in ('tag X', 'tag Y')
                group by blogpost_id
             ) tagmatch 
  on tagmatch.blogpost_id = b.Id
  order by tagmatch.matchCount desc

The aim is to rank blog posts by the number of matching tags so that a post with both tag X and tag Y comes above posts with just tag X.

I’ve got this so far:

 DetachedCriteria
                .For<Tag>("tag")
                .Add(Restrictions.In(Projections.Property<Tag>(x => x.Name), tags.ToArray()))
                .SetProjection(Projections.Group<Tag>(t => t.BlogPost))
                    .CreateCriteria("BlogPost")
                    .SetFetchMode("BlogPost", FetchMode.Eager)
                .AddOrder(Order.Desc(Projections.RowCount()));

However, the resulting query doesn’t join fetch BlogPost. Instead it returns just the ids, which leads to select n+1 when the BlogPosts are iterated.

public class BlogPost
{
  ...
  ISet<Tag> Tags {get; set;}
}

public class Tag
{
  BlogPost BlogPost { get; set; }
  string Name { get; set; }
}

This looks like a similar issue.

Is this now possible with NHibernate 3?

If not, is there an alternative solution?

I can change schema & domain model if necessary. I don’t want to use SQL or HQL if possible.

  • 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-24T20:32:21+00:00Added an answer on May 24, 2026 at 8:32 pm

    I know this question was put some time ago, but I want to do about the same thing, please take a look to my question here, and this guy here, maybe you can use the idea.

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

Sidebar

Related Questions

I would like to ask is there any way to achieve this functionality: I
I have elements structured roughly like this: http://jsfiddle.net/zyySd/ Is there any way to achieve
Is there any way to achieve compiling Node.js scripts as native code, like Hip-Hop
Is there any way to achieve this effect (3D CANVAS VIEW) for displaying an
This has been introduced in C# 4.0, but is there a way to achieve
Is there any possible way to achieve this? For instance, I have an I/O
Is there a way to achieve Backspace functionality in HTML? Do we have any
Is there a way to achieve the following in Maven? I want to exclude
Is there any way to achieve a += operator within the css operator (assuming
Update: Is there a way to achieve what I'm trying to do in an

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.