我想编写一个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
我该怎么做?
按照给定链路它会帮助你。 https://stackoverflow.com/questions/2643383/order-by-descending-based-on-condition –
'var query = q.OrderByDescending(...); if(..)query = query.ThenBy(...); else query = query.ThenByDescending(...); result = query.ToList();'或类似的东西。 – pinkfloydx33
对我来说没有意义,您将结果集列值与3进行比较并进行排序。相反,您应该在查询之前应用条件,然后对结果集进行排序。如果x = 3,查询应该返回OrderDate的顺序,否则按上次更新日期的顺序排序。 –