英文:
Using Orderby with conditional?
问题
你好,我正在尝试编写一个查询,按日期排序,但如果有 due_date,则使用该日期,如果没有 due_date,则使用 sample_date。这是我尝试的代码,但是不正确。
是否有办法在 orderby 或 thenby 中使用条件语句呢?
英文:
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.
is there a way to use a conditional in a orderby or a thenby or no?
答案1
得分: 2
Nulls在升序排序中会出现在第一条记录中。我们可以通过使用coalesce运算符来避免这种情况:
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论