【发布时间】:2023-03-21 08:38:01
【问题描述】:
tableA 包含用户最近访问过的项目列表,用户可以选择固定/删除列表。
我正在尝试的是列表必须按以下查询排序。
select * from tableA
order by ispinned desc,
case when ispinned = 1 then date end ASC,
case when ispinned = 0 then date end DESC
但在 LINQ 中它失败了。我尝试过的如下所示。
_lst = _lst.OrderByDescending( x => x.IsPinned).ThenByDescending(x=>x.AccessDate).ToList();
.
_lst = (from data in _lst
orderby data.IsPinned, data.IsPinned ? data.Date : data.Date descending
select data ).ToList();
【问题讨论】:
-
试试:
_lst = _lst.OrderByDescending(x => x.IsPinned).ThenBy(x=> x.IsPinned ? x.AccessDate : x.AccessDate.Ticks * -1 ).ToList(); -
我试过了,它显示编译时错误,因为 ternay 运算符后的数据类型不同
Error 17 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'long' -
@ Khanh 我认为可以进行一些修改,所以您能否发表您的评论作为答案,以便我接受并帮助其他人。
-
我发布了一个稍作修改的答案
标签: c# linq linq-to-sql