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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:51:23+00:00 2026-06-17T09:51:23+00:00

I have the following model: public class User { public virtual int Id {

  • 0

I have the following model:

public class User
{
    public virtual int Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual string Email { get; set; }
    public IList<SubmissionNote> Notes { get;set; }
}

public class SubmissionNote
{
    public virtual Int64 Id { get; set; }
    public virtual string Text { get; set; }
    public virtual DateTime CreatedOn { get; set; }
    public virtual User Creator { get; set; }
    public virtual Submission BelongsTo { get; set; }
}

public class Submission
{
    public virtual Int64 Id { get; set; }
    public virtual DateTime HappenedOn { get; set; }
    public virtual int StatusCode { get; set; }
}

I would like to write a query that brings all the Submissions whose last note was entered by some user or a list of users (identified by their names). I would appreciate your help for building this query using NHibernate QueryOver API. I have been able to create a query to bring all the last notes as follows:

SubmissionNote alias = null;
var lastNote = session.QueryOver<SubmissionNote>()
    .SelectList(list => list.
        SelectGroup(x => x.BelongsTo).WithAlias(() => alias.BelongsTo).
        SelectMax(x => x.CreatedOn).WithAlias(() => alias.CreatedOn).
        Select(x => x.Creator).WithAlias(() => alias.Creator).
        Select(x => x.Id).WithAlias(() => alias.Id).
        Select(x => x.Text).WithAlias(() => alias.Text))
    .TransformUsing(Transformers.AliasToBean<SubmissionNote>())
    .List();

Can I use the specified query as a subquery to produce the required result? Or do you suggest another solution?

  • 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-17T09:51:24+00:00Added an answer on June 17, 2026 at 9:51 am

    I would try something like this with a subquery (not tested though)

    SubmissionNote subNoteAlias = null, subNoteAliasMax =null;
    User userAlias = null;
    String[] userNames = new[]{"John","Joe"};
    
    var subquery =
        QueryOver.Of(() => subNoteAliasMax)
           .Where(subNote => subNote.BelongsTo.Id == subNoteAlias.BelongsTo.Id)
           .Select(Projections.Max<SubmissionNote>(subNote => subNote.CreatedOn));
    
    var result = session
     .QueryOver<SubmissionNote>(() => subNoteAlias)
     .WithSubquery.WhereProperty(subNote=>subNote.CreatedOn).Eq(subquery)
     .JoinQueryOver(
          subNote=>subNote.Creator
          ,()=>userAlias
          ,JoinType.InnerJoin
          ,Restrictions.In(Projections.Property(() => userAlias.Username ), userNames))
      .List();
    

    The generated SQL query would (very) roughly look like :

    SELECT SubmissionNote.*
      FROM SubmissionNote INNER JOIN User
      ON SubmissionNote.creatorId = User.userId
      AND User.userName in ('john','joe')
      WHERE SubmissionNote.CreatedOn = (SELECT MAX(CreatedOn) FROM SubmissionNote snAlias     
         WHERE snAlias.SubmissionId = SubmissionNote.SubmissionId)
    

    Hope this will help. There might be issues with the subquery returning a date instead of an id. LMK and I will give it a look.

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

Sidebar

Related Questions

I have the following model: public class Person { public int ID{get;set;} public string
I have the following model: public class Tag { public int Id { get;
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
I have the following model: public class Product { public int Id { get;
Yo i have the following nhibernate class: public class User { public virtual int
I'm using NHibernate 3.2 and have the following model: class User { public virtual
I have the following model entity: public class ScheduledTask { public virtual int ScheduledTaskId
I am using EntityFramework 4.1. I have the following model: public class User {
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
So I have the following model: public class Person { public String FirstName {

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.