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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:17:41+00:00 2026-06-11T08:17:41+00:00

I have two simple classes public class Blog { public Blog(){ Comments=new List<Comment>(); }

  • 0

I have two simple classes

public class Blog
{
  public Blog(){
    Comments=new List<Comment>();
  }
  public virtual Guid Id { get; set; }
  public virtual string Title { get; set; }
  public virtual string Text { get; set; }
  public virtual DateTime CreatedDate { get; set; }
  public virtual IList<Comment> Comments { get; set; }
}

and

public class Comment
{
  public virtual Guid Id { get; set; }
  public virtual string Author { get; set; }
  public virtual string Text { get; set; }
}

Mapped using AutoMap and all is great with the world. I can add and save entities no problems.

What I’d like to do is use QueryOver to get the number of comments per blog, but include those blogs where there are no comments, so in SQL:

SELECT b.Title,COUNT(c.ID) AS Comments
FROM Blogs b LEFT JOIN Comments c ON b.ID=c.BlogID

and get

Title  Comments
Blog 1 0
Blog 2 0
Blog 3 0
Blog 4 4
Blog 5 0

The closest I’ve got is

var results=session.QueryOver<Blog>()
.Left.JoinQueryOver<Comment>(b=>b.Comments)
.TransformUsing(new DistinctRootEntityResultTransformer())
.List<Blog>()
.Select(b => new {
Id = b.Id,
Title = b.Title,
Comments=b.Comments.Count
});

which gets the right answer, but the SQL runs as

SELECT b.Id,b.Title,c.ID,c.Author,etc... AS Comments
FROM Blogs b LEFT JOIN Comments c ON b.ID=c.BlogID

then does the counting at the client end, which doesn’t seem the most efficient way of doing it.

Can this be done with QueryOver or ICriteria? I’d rather not use hql if possible.

The entire solution is available at https://github.com/oharab/NHibernateQueriesSpike if you want to see all the config etc.

Cheers

B.

  • 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-06-11T08:17:43+00:00Added an answer on June 11, 2026 at 8:17 am

    Why do you always find the answer just after you’ve posted the question?

    The answer was the JoinAlias method, with an alias placeholder:

    Comment comment=null;
    var results=session.QueryOver<Blog>()
        .Left.JoinAlias(b=>b.Comments,()=>comment)
        .SelectList(
            list=>list
            .SelectGroup(b=>b.Id)
            .SelectGroup(b=>b.Title)
            .SelectCount(b=>comment.Id)
        )
        .List<object[]>()
        .Select(b => new {
                    Id = (Guid)b[0],
                    Title = (string)b[1],
                    Comments=(int)b[2]
                   });
    

    This does exactly as I expected it to.

    B.

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

Sidebar

Related Questions

I have two classes: class Player { public string Id { set; get; }
I have a simple Fluent NHibernate model with two related classes: public class Applicant
I have two classes: public class CourseModule { // attributes... List<Course> courses; public void
What I'm trying to do is fairly simple. I have two classes: public class
I have two very simple classes, one extends the other: public class LocationType implements
I have two simple classes generated by code first. public class Company { public
I have two simple Model classes and a ViewModel... public class GridItem { public
I have a very simple composite model made of two classes: Public Class ParentModelVM
I have two simple POCO classes; I'm trying to get the MyY property below
I have 3 simple classes: public abstract class Container implements WritableComparable<Container> {} //empty public

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.