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.
Explaining predicate:
Using the property info object, you get the value of the object x, with null parameters.
The parameters will be used for indexers, in this case, since this is a property info object.
But since the properties in this case are all simple properties, the second parameter should be null or an empty object array.