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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:57+00:00 2026-05-25T02:26:57+00:00

I have been searching for several hours now how to do this, but can’t

  • 0

I have been searching for several hours now how to do this, but can’t seem to find anything to help me.

Here is the database model:

enter image description here

This is the SQL query I am trying to run:

SELECT b.*, a.Assignments FROM Branch b LEFT JOIN (
        SELECT b.BranchID , COUNT(ab.BranchID) AS Assignments
        FROM Branch b LEFT JOIN AssignmentBranch ab ON b.BranchID = ab.BranchID
        GROUP BY b.BranchID
      ) a ON b.BranchID = a.BranchID

So, basically, I want to return a list of branches and a new column that represents the number of assignments for that branch.

Branch model

public class Branch : IEntity<int>
{
    public virtual int ID
    {
        get;
        set;
    }

    public virtual string Name { get; set; }

    public virtual IList<AssignmentBranch> Assignments { get; set; }

}

AssignmentBranch model

public class AssignmentBranch : IEntity<int>
{
    public virtual int ID
    {
        get;
        set;
    }

    public virtual DateTime AssignedOn { get; set; }

    public virtual Branch Branch { get; set; }
}

Here is my NHibernate configuration:

<class name="Branch" table="Branch">

<id name="ID" column="BranchID">
  <generator class="identity"></generator>
</id>

<property name="Name"/>

<bag name="Assignments" cascade="none" inverse="true">
  <key column="BranchID"/>
  <one-to-many class="AssignmentBranch"/>
</bag>

 <class name="AssignmentBranch" table="AssignmentBranch">

<id name="ID" column="AssignmentBranchID">
  <generator class="identity"></generator>
</id>

<property name="AssignedOn" />
<property name="FromDate" />
<property name="ToDate" />

<many-to-one name="Assignment" column="AssignmentID" />
<many-to-one name="Branch" column="BranchID" />

I have tried this a number of ways, but I can’t seem to find a way to join with a sub-query using QueryOver.

I tried like this:

 // aliases
 Branch branch = null; AssignmentBranch assignment = null;

 var subquery = QueryOver.Of<Branch>(() => branch)
     .Where(() => branch.Project.ID == projectID)
     .JoinQueryOver<AssignmentBranch>(() => branch.Assignments, ()=> assignment, 
                                   NHibernate.SqlCommand.JoinType.LeftOuterJoin)
     .SelectList(list => list
                        .SelectGroup(x=>x.ID)
                        .SelectCount(()=>assignment.ID)
                    );

     var query = session.QueryOver<Branch>(()=>branch)
                  .JoinAlias(???) // how can I join with a sub-query?
                  .TransformUsing(Transformers.AliasToBean<BranchAssignments>())
                  .List<BranchAssignments>();

Can anyone help me please? It doesn’t have to be with a sub-join exactly, maybe there is another better solution out there that I am missing…

Thank you,
Cosmin

  • 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-25T02:26:58+00:00Added an answer on May 25, 2026 at 2:26 am

    After reading hundreds of similar questions in here, I have found the answer: a correlated sub-query. Like this:

    // aliases
    Branch branch = null; AssignmentBranch assignment = null;
    
    var subquery = QueryOver.Of<AssignmentBranch>(() => assignment)
        .Where(() => assignment.Branch.ID == branch.ID)
        .ToRowCountQuery();
    
    var query = session.QueryOver<Branch>(() => branch)
         .Where(() => branch.Project.ID == projectID)
         .SelectList
         (
             list => list
             .Select(b => b.ID)
             .Select(b => b.Name)
             .SelectSubQuery(subquery)
         )
         .TransformUsing(Transformers.AliasToBean<BranchAssignments>())
         .List<BranchAssignments>();
    

    The similar question I got my answer from is this one.

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

Sidebar

Related Questions

I have been searching for the answer to this but cant seem to find
I have been searching all night for an answer to this but can't seem
I have been searching for a few hours and cant seem to find answer
I have been searching for several hours now and have so far had no
I have been searching and searching for this answer. But I cannot seem to
I have been searching for quite a bit now but I have never been
I've been searching all day and found several similar issues, but none have resolved
this one been puzzling me for a couple of searching hours. So I have
first time poster, but please help. I have been searching and reconfiguring for 6
I thought this question would have been asked before, but I couldn't find it

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.