LINQ Expression help with Func TEntity,TType
- by Chris Conway
I have a repository method that accepts an order by parameter in the form:
public IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity,string>> orderBy)
Now that works fine when trying to sort by a property of type string,
var entities = rep.Get(x => x.Name);
but what if i want to sort by double or int or any other type.
Doing something like var entities = rep.Get(x => x.Price); obviously throws a compile error saying I can't convert double to string.
How can I make this more generic so I can sort by any property in my entity, or at least the properties where the type implements IComparable or something similar?