I have a query where I need to have ordeby based on a querystring parameter .For example if sortby parameter is price , Query needs to change with price . If its rating than cha
Well, you could use a switch statement or something similar:
IQueryable query = ...;
switch (orderByParameter)
{
case "price":
query = query.OrderBy(x => x.Price);
break;
case "rating":
query = query.OrderBy(x => x.Rating);
break;
// etc
}
You could also do it with reflection, but assuming you have a limited number of fields to order by, this is quite possibly the simplest approach.