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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:36:48+00:00 2026-05-27T00:36:48+00:00

In short: Given an aggregate query (one with Max, Min, Count, etc) in NHibernate,

  • 0

In short: Given an aggregate query (one with Max, Min, Count, etc) in NHibernate, how can you modify the query to also return the full record associated with the aggregated value?

My example: I have 2 tables: People (primary key: PersonId) with a 1-to-many relationship to Events (primary key: EventId; other columns: PersonId, EventDate).

I want to select the last event per person and generate a list of these events. The SQL for last event per person would be something like SELECT PersonId, Max(EventDate) FROM ... GROUP BY PersonId. So far the NHibernate query looks like:

ICriteria criteria = session.CreateCriteria<Event>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.GroupProperty("PersonId"))
        .Add(Projections.Max("EventDate"))
    );

Now what I really need is the full event info. One solution, in theory, is to essentially join the above criteria to the Events table by PersonId and the max EventDate (easy enough in plain SQL). However I’m at a loss of how to perform this in NHibernate.

I’m open to any suggestion (HQL, LINQ, etc.) so long as it avoids stored procedures and views and is limited to 1 or just a few queries. Issuing a query per Person will not be scalable or performant in my case.

  • 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-27T00:36:49+00:00Added an answer on May 27, 2026 at 12:36 am

    I hope you are also open to QueryOver (this can be converted to ICriteria)…

    Event eventAlias = null;
    
    var topEventsByPerson = Session.QueryOver<Event>(() => eventAlias)
        .WithSubquery.WhereProperty(x => x.EventId).Eq(QueryOver.Of<Event>()
            .Where(x => x.Person == eventAlias.Person)
            .OrderBy(x => x.EventDate).Desc
            .Select(x => x.EventId)
            .Take(1))
        .List();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this stack net4/IIS7.x MVC3 nHibernate Unity But short of starting from scratch and
Short description : given a queryset myQueryset , how do I select max(myfield) without
Given two shorts ( System.Int16 ) short left = short.MaxValue; short right = 1;
How can you sort the following word by the given rule? Example data is
I've found how to sort query results by a given field in a Lucene.Net
Given this Short (signed): &Hxxxx I want to: Extract the most right &HxxFF as
Given a function prototype, and a type definition: int my_function(unsigned short x); typedef unsigned
I've been given a short amount of time (~80 hours to start with) to
I've been given a bunch of messy code and a short time limit (no
In short: Given that a is coprime to b if GCD(a,b) = 1 (where

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.