Reputation: 2020
I have records that are given a class that i need to order by.
The values are simple 0,1,2. The problem is the client needs the records to appear in a order based on there class name not class value. The order should appear as seen below.
class (TBD)= 0
class (2/3)= 2
class (1)= 1
Using System.Linq.Dynamic
i have been able to pass columns and there direction into my OrderBy()
. Is there a way to pass in a expression that would force the sort order to be
Ascending: class (TBD)= 0, class (2/3)= 2 , class (1)= 1
Descending: class (1)= 1, class (2/3)= 2, class (TBD)= 0
I have tried asc:
sort = "(Class = 0) desc, (Class = 2) desc, (Class = 1) desc";
desc:
sort = "(Class = 1) desc, (Class = 2) desc, (Class = 0) desc";
But i get Operator '=' incompatible with operand types 'String' and 'Int32'
Before that i was using '==' and got a similar message.
searchUniq = searchUniq.OrderBy(sort).Skip(skip).Take(25);
Upvotes: 2
Views: 576
Reputation: 71573
Your string is not a valid sort syntax for Dynamic Linq. Try:
sort = "iif(Class == 1, 0, iif(Class == 2, 1, 2))";
This uses the inline if expression available in Dynamic Linq (at least one flavor of it; see here for more documentation) to transform the Class values into something intuitively orderable. The vanilla SQL equivalent is to order by a case statement.
Upvotes: 3