2017-07-31 34 views
0

我想编写一个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 

我该怎么做?

+0

按照给定链路它会帮助你。 https://stackoverflow.com/questions/2643383/order-by-descending-based-on-condition –

+0

'var query = q.OrderByDescending(...); if(..)query = query.ThenBy(...); else query = query.ThenByDescending(...); result = query.ToList();'或类似的东西。 – pinkfloydx33

+0

对我来说没有意义,您将结果集列值与3进行比较并进行排序。相反,您应该在查询之前应用条件,然后对结果集进行排序。如果x = 3,查询应该返回OrderDate的顺序,否则按上次更新日期的顺序排序。 –

回答

1

您可以在一个OrderBy做到这一点,例如:

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

其中一个命令是asc,接下来是desc –

+0

这没有任何意义。您需要[编辑](https://stackoverflow.com/posts/45413080/edit)您的问题,并提供示例数据和预期输出。 – DavidG