Aggregate MAX throws "value cannot be null" when no rows are returned (sql returns null)

Description

A simple NHibernate 3.0 linq (the version released with NH3.0) query:

Works perfectly when the particular unit has reasons, however throws "value cannot be null. Parameter name: item" when no rows are returned. Also the confusing outer exception with message "SQL: SQL not available" is used.

The generated SQL seems correct, but it doesnt seem to handle NULL result.

I saw a BUG NHQ-10, that seemed similar and was fixed, however it doesnt seem so or this is something different. Doesnt seem proper behaviour, should return null or 0 (default(int)).

Environment

None

Activity

Alex Zaytsev 
July 13, 2018 at 10:53 AM

Moved here.

Not an issue. Prior (#584) it was throwing an ArugmentNullException. After it throws InvalidOperationException which aligns with the behaviour of the linq-to-objects.

CBP 
February 15, 2013 at 5:56 AM

I am getting the same error for SUM and FutureValue:

Session.QueryOver<Order>()
.Select(Projections.Sum<Order>(x => x.Amount))
.FutureValue<Money>();

Change the FutureValue to nullable Money? fixes the problem, but is a hassle.

Oskar Berggren 
November 13, 2012 at 10:33 AM

SQL MAX() is defined to return NULL when there are no input rows. This behavior isn't hidden by either L2SQL or L2EF, so probably NH shouldn't either.
http://stackoverflow.com/questions/3274196/entity-framework-calling-max-on-null-on-records

A more informative exception message would be nice though.

Taavi Kõosaar 
February 28, 2011 at 11:26 PM

Thank you, your code worked (tried without the Take(1)).

However, why dident the other QueryOver code work?

PhillipH 
February 25, 2011 at 5:29 PM

Taavi - I've written it as QueryOver for you.

sortOrder = _nHibernateSession.QueryOver<DeclinedReason>()
.Where(dr => dr.Unit.Id == newReason.Unit.Id)
.SelectList(x => x.SelectMax(y => y.SortOrder))
.Take(1)
.SingleOrDefault<int>();

Not an Issue

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created February 17, 2011 at 7:38 AM
Updated July 13, 2018 at 10:53 AM
Resolved July 13, 2018 at 10:53 AM
Who's Looking?