I have:
IQueryable<T> collection
var sortSelectorParameter = Expression.Parameter(typeof(T), "c");
var sortSelector = GetPropertyOrField<T>(sortSelectorParameter, "SomeId");
collection = collection.OrderByDescending(Expression.Lambda<Func<T, object>>(sortSelector, sortSelectorParameter));
This works fine if the parameter (SomeId) is a string. However if it's an integer (or presumably other types as well) then I get an exception saying it cannot convert int to an object.
If I try an Expression.Convert
I get another exception later on saying entity framework cannot convert it to a string as it doesn't support conversions.
If I change Expression.Lambda<Func<T, object>>
to Expression.Lambda<Func<T, int>>
then it works perfectly but only for integer properties.
I am not sure how to make it support all types with some monstrous switch statement. Can anyone offer any advice on this?