Possible Duplicate:
LINQ: orderby with dynamic string parameter
i'm sorting a IEnumarable with an OrderBy() clause. I'v a list with string with a value which contains the field I wanna order by. Now I'm using a switch statement for each property name.
swich (propertyname)
case "name":
list = list.OrderBy(l=>l.name).ToList();
break;
case "property":
list = list.OrderBy(l=>l.property).ToList();
.....
is there a simple solution to use the string "propertyname" as an attribute in the orderby clause?
As I've done it I get an solution that is far from desirable. Not only is it more work to code each property, but if in the future any attribute is added this update wil be forgotten in the function i'm writing.
hope someone has got a better solution.