【问题标题】:Entity Framework Order by descending by row field in query实体框架按查询中的行字段降序排列
【发布时间】: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


【解决方案1】:

您可以在单个OrderBy 中执行此操作,例如:

var results = q.OrderByDescending(x => 
    x.StatusId == 3 ? x.ReserveDate : x.LastUpdateDate)

【讨论】:

  • 一个命令是asc,下一个是desc
  • 这没有任何意义。您需要edit您的问题并提供示例数据和预期输出。
猜你喜欢
  • 1970-01-01
  • 2016-08-20
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多