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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:50:59+00:00 2026-06-15T03:50:59+00:00

I am working with an NHibernate project that has a method that returns an

  • 0

I am working with an NHibernate project that has a method that returns an IQuery object.
I want to find the oldest car with a particular colour.

At the moment it returns the oldest car only if you if you specify the colour correctly (or don’t specify it at all).

I can see roughly what I am doing wrong (I am getting the maxAge for the whole table and then adding it as a restriction on the existing IQueryOver).

How do I change the code to get the max age just for existing IQueryOver?

private IQueryOver GetFilteredQuery()
{
    var query = Session.QueryOver<Car>();

    if (this.Colour != nulI)
    {
        query.Where(x => x.Colour == this.Colour));
    }

    if (this.GetOldestCar == true)
    {
        QueryOver<Car> maxAge= QueryOver.Of<Car>()
            .SelectList(c => c.SelectMax(x => x.Age));

        query.Where(Subqueries.WhereProperty<Car>(i => i.Age).Eq(maxAge));
    }

    return query;
}
  • 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-15T03:51:00+00:00Added an answer on June 15, 2026 at 3:51 am

    To get the oldest Car, you may try :

    var query = Session.QueryOver<Car>().OrderBy(x=>x.Age).Desc().Take(1);
    

    This would lead to :

    private IQueryOver GetFilteredQuery()
    {
        var query = Session.QueryOver<Car>();
    
        if (this.Colour != null)
        {
            query.Where(x => x.Colour == this.Colour));
        }
    
        if (this.GetOldestCar)
        {
            query.OrderBy(x=>x.Age).Desc().Take(1)
        }
    
        return query;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that has a rich object model with various sets
I am using Fluent NHibernate on a project that has an Oracle 10g database.
I'm working on a Grails 1.0.4 project that has to be released in less
I'm working on an nHibernate project that uses an Access Database as the datasource
The project that I'm working on has the following setup: JPA 2.0 (Hibernate 4
I'm working on setting up NHibernate for a project and I have a few
I am using NHibernate for my project. i am quite a beginner at working
I am working on my first NHibernate project, and have the following setup/requirement. I
The project I'm working on has a legacy database with lots of information in
i'm working on a small project that is supposed to allow basic searches of

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.