I have written the LINQ
query as following but just wondering if i can make it more generic and get rid of the hard coded values.
switch (sortOrder)
{
case "DESC":
if(sortBy == "A")
query = query.OrderByDescending(x => x.Field1);
if(sortBy == "B")
query = query.OrderByDescending(x => x.Field2);
if (sortBy == "C")
query = query.OrderByDescending(x => x.Field3);
break;
default:
if(sortBy == "A")
query = query.OrderBy(x => x.Field1);
if(sortBy == "B")
query = query.OrderBy(x => x.Field2);
if (sortBy == "C")
query = query.OrderBy(x => x.Field3);
break;
}
The link mentioned as duplicate is not the answer i am looking for. In that question user asked by sorting 1st of ASC and then Desc.
In my case i have to either sort ASC or DESC.