I can construct Linq Query dynamically with orderby statement like 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;
// ... and more cases
}
var Result = Query.ToList();
However, if there is a need to be ordered descending (depends on user selection in the UI), I will have to construct another switch statement duplicate all the cases just to add the "descending" keyword after the 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 for me to add just the descending keyword dynamically to the query?