views:

37

answers:

1

Hi, I have a PrivateMessage class and I want to get list of PMs for user sorted chronologically either by CreationDate or LastAnswerDate (depending on which is more recent) using Criteria API.

How to sort by max of these two properies in Criteria API? My code looks similar to following:

var dc = DetachedCriteria.For<PrivateMessage>();
...
dc.AddOrder(new Order("???");
return (IList<PrivateMessage>)FindAll(typeof(PrivateMessage), dc);

CreationDate is DateTime and LastAnswerDate is DateTime?.

Thanks!

+1  A: 
Order.Desc(
    Projections.Conditional(
        Restrictions.GtProperty("CreationDate", "LastAnswerDate"),
                                Projections.Property("CreationDate"),
                                Projections.Property("LastAnswerDate"))))
Diego Mijelshon
Thanks :). I solved that by coalesce since one property is nullable but your solution will work always.
Episodex