Search code examples
c#nhibernate

Using Orderby with conditional?


Hello I am trying to get a query to order by dates but if there is a due_date to use that if there isn't a due_date it needs to use sample_date. This is what i tried but it isnt correct.

enter image description here

is there a way to use a conditional in a orderby or a thenby or no?

expecting: enter image description here

what i got: enter image description here


Solution

  • Nulls appear in ascending sorting as first records. We can avoid this by coalesce operator:

    session.Query<ActiveTestListResult>()
        .OrderByDescending(x => x.lab_int)
        .ThenBy(x => x.due_date ?? x.sample_date)
        .ThenBy(x => x.sample_id)
        .ThenBy(x => x.batch_id)