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?
Use OrderByDescending to sort in Descending order. . ToList(); Use ThenBy or ThenByDescending clause to sort the results on multiple fields.
OrderByDescending operator is used to rearranging the elements of the given sequence in descending order. It does not support query syntax in C# and VB.Net.
Add sorting functionality to the Index method The query string value is provided by ASP.NET MVC as a parameter to the action method. The parameter is a string that's either "Name" or "Date", optionally followed by an underscore and the string "desc" to specify descending order. The default sort order is ascending.
You can do this in a single OrderBy
, for example:
var results = q.OrderByDescending(x =>
x.StatusId == 3 ? x.ReserveDate : x.LastUpdateDate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With