In order to be able to dynamically use strings in LINQ, I use the following helper function I built in order to construct a Lambda expression from a string:
public static Func<ObjectToOrder, object> GetSortExpression<ObjectToOrder> (string field) {
var param = Expression.Parameter(typeof(ObjectToOrder), "objectToOrder");
var mySortExpression = Expression.Lambda<Func<ObjectToOrder, object>>(Expression.Property(param, field), param);
return mySortExpression.Compile();
}
I'll explain how this function works in the following example: I have a class Person with three of attributes:
class Person {
public string Name {get;set;}
public int Age {get;set;}
public EnumerateType Type {get;set;}
}
enum EnumerateType {
Normal = 1,
Premium = 2
}
Then inside a List of Person, I would use my helper function to order by any attribute this way:
List<Person> people = new List<Person>();
//Create people in the list
//...
people = people.OrderBy(GetSortExpression<Person>("Name")).ToList();
people = people.OrderBy(GetSortExpression<Person>("Age")).ToList();
people = people.OrderBy(GetSortExpression<Person>("Type")).ToList();
With the attributes Name and Age it works fine, but with the Type attribute it raises the following exception when executing "var mySortExpression = Expression.Lambda>(Expression.Property(param, field), param);" of the helper function:
Cannot use a expression of type 'EnumerateType' for the returned value type 'System.Object'
Why is that so? What should I do in order to solve it and be able to create a lambda expression for enumerates (I thought internally enumerates are integers)?
Thank you
EDIT: I've noted that it also crashes with any DateTime attribute.