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

  • Home
  • SEARCH
  • 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 8454633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:04:38+00:00 2026-06-10T12:04:38+00:00

I need to execute a query that joins two tables, groups by a value,

  • 0

I need to execute a query that joins two tables, groups by a value, and returns the group key and a count.

CREATE TABLE Message(
  ID INT NOT NULL
, Note NVARCHAR(128));

CREATE TABLE Message_Users(
  MessageID INT NOT NULL
, UserID NVARCHAR(128) NOT NULL);

I need to execute this query:

SELECT mu.UserID, COUNT(m.ID)
FROM Message m
JOIN Message_Users mu
   ON m.ID = mu.MessageID
GROUP BY mu.UserID;

How can I do this using NHibernate? The Message table is mapped as follows:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataModel" namespace="DataModel.Message">
  <class name="Message" table="Message">
    <id name="ID">
      <generator class="native" />
    </id>
    <bag name="AssociatedUsers" table="Message_Users" lazy="false">
      <key column="MessageID" />
      <element column="UserID" type="System.String" />
    </bag>
  </class>
</hibernate-mapping>

I have looked at several other questions, but none meet quite this use case (plenty of GroupBy, but not with an association; plenty of join, but not with a group by)

Two directions I have started going in:

// First attempt

// Doesn't work as message.AssociatedUsers is a collection, so mu.Key is actually a collection
var query = transactionContext.Session.Query<Message>();    
var r = from message in query
        group message by message.AssociatedUsers into mu
        select new {UserID = mu.Key, Count = mu.Count()};

// Second attempt

var query = transactionContext.Session.QueryOver<Message>();
var join = query.Right.JoinQueryOver<string>(_message => _message.AssociatedUsers);
var select = join.Select(
      // Select generic parameter doesn't seem to be related to the type of the join (<Message, string>)
    Projections.Group<string>(... 
  • 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-10T12:04:39+00:00Added an answer on June 10, 2026 at 12:04 pm

    not tested

    // Option 1
    var r = from message in Session.Query<Message>()
            from userid in message.AssociatedUsers
            group userid by userid into g
            select new { UserID = g.Key, Count = g.Count() };
    
    // Option 2
    string userId = null;
    var r = Session.QueryOver<Message>()
        .JoinQueryOver<string>(m => m.AssociatedUsers, () => userId)
        .SelectList(list => list
            .SelectGroup(() => userid)
            .Select(Projections.Count()))
        .List<object[]>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that joins two tables. One table has a column that
is it posssible to execute a delete query statement that joins the same table,
I have a query that I need to execute that I do not know
I need to execute a SQL Query between two databases. Example: SELECT * from
I need to execute a query that is highly dependent on several conditions what
I currently need to execute a database query that involves data from three separate
I have a query that I need to execute against CRM. It is the
I have a query with subquery in it. the subquery returns the value, that
I need execute an SQL query in ORACLE it takes a certain amount of
I need to execute the following grep query as an argument for konsole (the

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.