Entity Framework Order by descending by row field in query

c# entity-framework linq

Question

I want to write a EF query which does order by ascending or descending based on condition. Following is the my pseudo code:

  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

How can i do this?

1
3
7/31/2017 10:14:08 AM

Popular Answer

You can do this in a single OrderBy, for example:

var results = q.OrderByDescending(x => 
    x.StatusId == 3 ? x.ReserveDate : x.LastUpdateDate)
4
7/31/2017 10:18:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow