I need to set the key in the OrderBy clause using linq , where the key is a property of an object. I only have the property name with me. How can i set the property dynamically.
class Obj
{
string Level
{
get;
set;
}
}
List<Obj> objList;
objList.OrderByDescending(x => x.Level); => "here i only have property name."
Hope any one can help
Thanks Sunil
You can do this by Extension Methods and Creating Expression Trees dynamically like
and for OrderBy
and you can combine these two in one like
Then you can call it like
objList.OrderBy("Level","asc")
//For AscendingobjList.OrderBy("Level","desc")
//For DescendingHope It will help