Exemplo n.º 1
0
        private static IOrderedQueryable <T> DynamicOrderBy <T, TKey>(IQueryable <T> query, OrderByItem orderBy)
        {
            var keySelector = (Expression <Func <T, TKey> >)orderBy.Expression;

            var orderedQuery = query as IOrderedQueryable <T>;

            if (orderedQuery != null && query.Expression.Type != typeof(IQueryable <T>))
            {
                return(orderBy.Descending
                    ? orderedQuery.ThenByDescending(keySelector)
                    : orderedQuery.ThenBy(keySelector));
            }

            return(orderBy.Descending
                ? query.OrderByDescending(keySelector)
                : query.OrderBy(keySelector));
        }
Exemplo n.º 2
0
        private static IOrderedQueryable <T> DynamicOrderBy <T>(IQueryable <T> query, OrderByItem orderBy)
        {
            var func = MethodHelpers.GetMethodInfo(DynamicOrderBy <T, object>, query, orderBy)
                       .GetGenericMethodDefinition()
                       .MakeGenericMethod(typeof(T), orderBy.Expression.PropertyInfo().PropertyType);

            return((IOrderedQueryable <T>)func.Invoke(null, new object[] { query, orderBy }));
        }