I want to be able to use the firstby / thenby to sort as follows:
allOrders().sort(s => s.ProductName, s => s.OrderDate)
So, using this post as inspiration , I wrote this extension method, which compiles fine:
public static IQueryable<T> sort<T>(this IQueryable<T> entities, params Expression<Func<T, object>>[] predicates) where T : class { var sorted = entities.OrderBy(predicates[0]); for (int i = 1; i < predicates.Length; i++) sorted = sorted.ThenBy(predicates[i]); return sorted; }
And I also tried this version of succint, which also compiles:
public static IQueryable<T> sort<T>(this IQueryable<T> entities, params Expression<Func<T, object>>[] predicates) where T : class { return predicates.Skip(1).Aggregate( entities.OrderBy(predicates[0]), (aggregate, currentPredicate) => aggregate.ThenBy(currentPredicate)); }
However, if I try to sort by DateTime , I get this exception:
Cannot enter type "System.DateTime" to enter "System.Object". LINQ to Entities only supports EDM listing of primitive or enumerated types.
What am I doing wrong? I am using EF5.
source share