Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading