Possible duplicate:
LINQ: orderby with dynamic string parameter
I sort IEnumarable with an OrderBy () clause. I'v a list with a string with a value that contains the field that I want to order. Now I use the 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 an orderby clause?
As I did, I get a solution that is far from what I want. Not only does this work more for encoding each property, but if any attribute is added in the future, this update will be forgotten in the function I'm writing.
hope someone has a better solution.
source
share