实体框架按查询中的行字段降序排序

ahm*_*d-r 6 c# linq entity-framework

我想编写一个 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
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Dav*_*idG 7

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

var results = q.OrderByDescending(x => 
    x.StatusId == 3 ? x.ReserveDate : x.LastUpdateDate)
Run Code Online (Sandbox Code Playgroud)