Possible Duplicate:
Dynamic LINQ OrderBy
switch (sort) {
case "Title":
queryResults = queryResults.OrderBy(r => r.Title);
break;
default:
queryResults = queryResults.OrderBy(r => r.LastName);
break;
Is there any way I can get rid of the switch block above?
Can I do some thing like:
queryResults = queryResults.OrderBy(r => r."sort");
or
queryResults = queryResults.OrderBy(r => r.sort);
If you wanted to do this completely dynamic, you could use some reflection (simple example):
I wouldn’t consider this the nicest solution in any case though. Whether this really makes sense for you depends on where you get the property name from (if you get it from reflection, too, or not) and how many properties there are.