【发布时间】:2017-07-31 10:14:08
【问题描述】:
我想编写一个 EF 查询,它根据条件按升序或降序进行排序。以下是我的伪代码:
var result= q.OrderByDescending(x => x.StatusId == 3)
then if( x.StatusId == 3)
then order by x.ReserveDate
else
then order by descending x.LastUpdateDate
我该怎么做?
【问题讨论】:
-
按照给定的链接,它会帮助你。 stackoverflow.com/questions/2643383/…
-
var query=q.OrderByDescending(...); if (..) query=query.ThenBy(...); else query = query.ThenByDescending(...); result=query.ToList();或类似的东西。 -
对我来说没有意义,您将结果集列值与 3 进行比较并进行排序。相反,您应该在查询之前应用条件,然后对结果集进行排序。 if x=3 查询应按 ReserveDate 返回 order by Last Update Date desc.
-
忽略我的评论。我错过了您的 if 语句需要记录本身的状态这一事实。在那种情况下,我同意这有点没有意义
-
你的previous question有什么区别?
标签: c# entity-framework linq