Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max in linq to NHibernate for not exist data in database

I have a query by linq to NHibernate.

var q = SessionInstance.Query<Request>().Max(e => e.Code);

If Request table has no rows, execution of this query raises GenericADOException with this message :

{"Could not execute query[SQL: SQL not available]"}

{"Value cannot be null.\r\nParameter name: item"}

What should I do?

like image 740
Ehsan Avatar asked Jun 12 '12 13:06

Ehsan


2 Answers

Try this

SessionInstance.Query<Request>().Max(x => (int?)x.Code);
like image 54
Marian Ban Avatar answered Nov 15 '22 19:11

Marian Ban


I think this should work with Linq-to-Nhibernate:

var q = SessionInstance.Query<Request>().Select(e => e.Code)
    .DefaultIfEmpty().Max();

or maybe DefaultIfEmpty(<some value>).

like image 45
Gert Arnold Avatar answered Nov 15 '22 19:11

Gert Arnold