Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate ICriteria - Does the sort allow for null?

Using NHibernate ICriteria and adding .AddOrder ... I want to sort by a property that is sometimes null with all the populated ones at the top. Will .AddOrder allow me to do this? If not is there an alternative?

The sorting options for ILists leave a lot to be desired.

like image 856
Sara Chipps Avatar asked Mar 02 '23 06:03

Sara Chipps


2 Answers

If you use something similar to:

IList cats = sess.CreateCriteria(typeof(Cat))
    .AddOrder( Order.Desc("PropertyName") )
    .List();

The objects with NULLs for the given property will be last in the list.

(Taken in part from the NHibernate documentation.)

like image 107
wprl Avatar answered Mar 12 '23 15:03

wprl


You should get the non-null values first by using that method. We use sorting in that way on my project, and have not had any issues with the null values... they get listed at end.

like image 44
Elie Avatar answered Mar 12 '23 16:03

Elie