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;
}
To get the oldest Car, you may try :
This would lead to :