EF Core 如何实现在 OrderBy 时如果值为 null 则排在非 null 值之后?对应的数据库是 SQL Server
var result = list
.OrderByDescending(i => i.date.HasValue)
.ThenBy(i => i.date);
是的,应该用 OrderByDescending
生成的 SQL 语句
ORDER BY CASE WHEN [b].[Date] IS NOT NULL THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END DESC, [b].[Date]
var result = query
.OrderBy(i => i.Order == null ? 1 : 0)
.ThenBy(i => i.Order);
这样也可以,生成的 SQL 语句更简洁
ORDER BY CASE WHEN [b].[Order] IS NULL THEN 1 ELSE 0 END, [b].[Order]