LINQ Expression help with Func TEntity,TType

Posted by Chris Conway on Stack Overflow See other posts from Stack Overflow or by Chris Conway
Published on 2010-12-29T14:44:42Z Indexed on 2010/12/29 14:54 UTC
Read the original article Hit count: 171

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ