I can build Linq Query dynamically with an orderby statement, as shown below:
var Query = from q in Db.TblUsers select q; switch (Order) { case "Name": Query = from q in Query orderby q.Name select q; break; case "DOB": Query = from q in Query orderby q.DOB select q; break;
However, if there is a need to sort in descending order (it depends on the user choice in the user interface), I will have to build another switch statement in order to duplicate all cases in order to add a βdescendingβ keyword after orderby.
Example:
if (ascending) { switch (Order) { case "Name": Query = from q in Query orderby q.Name select q; break; // .... } } else { switch (Order) { case "Name": Query = from q in Query orderby q.Name descending select q; break; // .... } }
Is there a way to add a dynamic expression for a top-down keyword in a query?
source share