使用Orderby与条件语句?

huangapple go评论62阅读模式
英文:

Using Orderby with conditional?

问题

你好,我正在尝试编写一个查询,按日期排序,但如果有 due_date,则使用该日期,如果没有 due_date,则使用 sample_date。这是我尝试的代码,但是不正确。

是否有办法在 orderby 或 thenby 中使用条件语句呢?

期望结果:
使用Orderby与条件语句?

实际结果:
使用Orderby与条件语句?

英文:

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.

使用Orderby与条件语句?

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

expecting:
使用Orderby与条件语句?

what i got:
使用Orderby与条件语句?

答案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&lt;ActiveTestListResult&gt;()
	.OrderByDescending(x =&gt; x.lab_int)
	.ThenBy(x =&gt; x.due_date ?? x.sample_date)
	.ThenBy(x =&gt; x.sample_id)
	.ThenBy(x =&gt; x.batch_id)

huangapple
  • 本文由 发表于 2023年8月8日 20:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定