Dynamic OrderBy in LINQ
Have you ever found yourself writing this code?
switch (columnName)
{
case "Name":
return objList.OrderBy(o => o.Name);
case "Address":
return objList.OrderBy(o => o.Address);
... etc ...
}
If so then you have probably wished you could do this:
return objList.OrderBy(columnName);
However, that's not going to work because the LINQ method is looking for an expression tree rather than a column name. There are a few libraries out there such as the Dynamic LINQ Library, but if that feels a little heavyweight for you then the following solution may help.
This class is a generic sorter for collections of type T. There are two...