Possible Duplicate:
Dynamic LINQ OrderBy
How can I pass a value to an OrderBy or OrderByDescending statement in Linq? I am trying to get the property name dynamically:
x.GetType().GetProperty(sortField)
Which doesnt seem to work. Any Ideas?
private void DynamicSort(ref List<Quote> myQuotes, String sortField, String direction)
{
if(direction == "ASC"){
this.grdViewDrafts.DataSource = myQuotes.OrderBy(x => x.GetType().GetProperty(sortField)).ToList();
}else{
this.grdViewDrafts.DataSource = myQuotes.OrderByDescending(x => x.GetType().GetProperty(sortField)).ToList();
}
}
Solution:
this.grdViewDrafts.DataSource = myQuotes.OrderBy(x => x.GetType().GetProperty(sortField).GetValue(x, null)).ToList();