Exemplo n.º 1
0
        private IOrderedQueryable <TEntity> ApplyOrderBy <TKey>(IQueryable <TEntity> query, Expression <Func <TEntity, TKey> > expr, SortColumn.Directions direction)
        {
            switch (direction)
            {
            case SortColumn.Directions.Ascending:
                return(query.OrderBy(expr));

            case SortColumn.Directions.Descending:
                return(query.OrderByDescending(expr));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        private IOrderedQueryable <TEntity> StrongTypeOrderBy(Expression <Func <TEntity, object> > orderByExpression, IOrderedQueryable <TEntity>?orderedQuery, IQueryable <TEntity> query, SortColumn.Directions direction)
        {
            var exprbody = orderByExpression.Body;

            if (exprbody.NodeType == ExpressionType.Convert)
            {
                exprbody = ((UnaryExpression)exprbody).Operand;
            }

            var expressionAsLambda = Expression.Lambda(exprbody, orderByExpression.Parameters);

            if (exprbody.Type == typeof(Guid))
            {
                var asexpression = (Expression <Func <TEntity, Guid> >)expressionAsLambda;
                orderedQuery = orderedQuery == null
                    ? ApplyOrderBy(query, asexpression, direction)
                    : ApplyThenBy(orderedQuery, asexpression, direction);
            }
            else if (exprbody.Type == typeof(DateTime))
            {
                var asexpression = (Expression <Func <TEntity, DateTime> >)expressionAsLambda;
                orderedQuery = orderedQuery == null
                    ? ApplyOrderBy(query, asexpression, direction)
                    : ApplyThenBy(orderedQuery, asexpression, direction);
            }
            else if (exprbody.Type == typeof(int))
            {
                var asexpression = (Expression <Func <TEntity, int> >)expressionAsLambda;
                orderedQuery = orderedQuery == null
                    ? ApplyOrderBy(query, asexpression, direction)
                    : ApplyThenBy(orderedQuery, asexpression, direction);
            }
            else if (exprbody.Type == typeof(string))
            {
                var asexpression = (Expression <Func <TEntity, string> >)expressionAsLambda;
                orderedQuery = orderedQuery == null
                    ? ApplyOrderBy(query, asexpression, direction)
                    : ApplyThenBy(orderedQuery, asexpression, direction);
            }
            else
            {
                throw new ArgumentOutOfRangeException();
            }

            return(orderedQuery);
        }