【问题标题】:Order by bigger of two Sql columns按两个 Sql 列中的较大者排序
【发布时间】:2020-07-08 02:07:37
【问题描述】:

我有一个包含 2 列的 SqlTable,DateInsert(不可为空)和 DateUpdate(可空)。

有什么方法可以使用 c# linq 按两列的最新日期时间排序??

我正在尝试使用 LINQ 在 C# IQueryable 中进行操作,因此将查询保存到内存列表中并稍后重新排序不是选项,我需要 sqlserver 可以理解并一步完成查询。

我尝试使用 query.OrderBy(x=>x.DateUpdate).ThenBy(x=>x.DateInsert) 但效果不佳...

预期的结果就像下一个例子:

Item1: Insert: 2020-01-01 Update: null
Item2: Insert: 2019-01-01 Update: 2020-03-01
Item3: Insert: 2019-01-01 Update: 2019-05-01

Ordered Result: Item2->Item1->Item3

感谢您的帮助!

【问题讨论】:

    标签: c# sql-server linq sql-order-by iqueryable


    【解决方案1】:

    获取最大值并按此排序:

    query.OrderBy(x=> x.DateUpdate>x.DateInsert?x.DateUpdate:x.DateInsert);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 2015-09-01
      • 2015-07-19
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多