我想编写一个EF查询,根据条件进行升序或降序排序。以下是我的伪代码:
var result= q.OrderByDescending(x => x.StatusId == 3)
if( x.StatusId == 3)
then order by x.ReserveDate
if( x.StatusId != 3 )
then order by descending x.LastUpdateDate
我该怎么做呢?
更新
这与被标记为重复问题的q = condition ? q.OrderBy(..) : q.OrderByDescending(..)
不同,排序顺序基于行内值而不是查询外部标志。
Select * from myTable Where StatusId == 3 OrderBy ReverseDate Union Select * from my Table Where StatusId != 3 OrderBy LastUpdateDate Desc
我相信在LINQ中,Concat方法等同于sql查询中的Union。 - BoeseB