Esempio n. 1
0
 public virtual SortBuilder <T> Take(int count)
 {
     _baseBuilder.Take(count);
     return(this);
 }
Esempio n. 2
0
        /// <summary>
        /// Handles OrderBy.
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected override System.Linq.Expressions.Expression VisitMethodCall(MethodCallExpression expression)
        {
            switch (expression.Method.Name)
            {
            case "Where":
                var quote = expression.Arguments[1] as UnaryExpression;
                if (quote != null)
                {
                    var predicate = quote.Operand as Expression <Func <TEntity, bool> >;
                    _sortBuilder = _queryBuilder.Where(predicate);
                }
                break;

            case "OrderBy":
            case "ThenBy":

                this.Visit(expression.Arguments[0]);

                var keySelectorAsc = expression.Arguments[1] as UnaryExpression;
                if (keySelectorAsc != null)
                {
                    if (_sortBuilder == null)
                    {
                        _sortBuilder = _queryBuilder.AddSortExpression(keySelectorAsc.Operand, SortDirection.Asc);
                    }
                    else
                    {
                        _sortBuilder.AddSortExpression(keySelectorAsc.Operand, SortDirection.Asc);
                    }
                }
                break;

            case "OrderByDescending":
            case "ThenByDescending":

                this.Visit(expression.Arguments[0]);

                var keySelectorDesc = expression.Arguments[1] as UnaryExpression;
                if (keySelectorDesc != null)
                {
                    if (_sortBuilder == null)
                    {
                        _sortBuilder = _queryBuilder.AddSortExpression(keySelectorDesc.Operand, SortDirection.Desc);
                    }
                    else
                    {
                        _sortBuilder.AddSortExpression(keySelectorDesc.Operand, SortDirection.Desc);
                    }
                }
                break;

            case "Skip":
                this.Visit(expression.Arguments[0]);
                int skipVal = (int)GetConstantValue(expression.Arguments[1]);
                _queryBuilder.Skip(skipVal);
                break;

            case "Take":
                this.Visit(expression.Arguments[0]);
                int takeVal = (int)GetConstantValue(expression.Arguments[1]);
                _queryBuilder.Take(takeVal);
                break;

            case "FirstOrDefault":
            case "SingleOrDefault":
                this.Visit(expression.Arguments[0]);
                _queryBuilder.SetSingleOrFirstAllowNull(true);
                break;

            case "First":
            case "Single":
                this.Visit(expression.Arguments[0]);
                _queryBuilder.SetSingleOrFirstAllowNull(false);
                break;

            default:
                string msg = string.Format("'{0}' expressions are not currently supported in the where clause expression tree parser.", expression.Method.Name);
                throw new NotSupportedException(msg);
            }

            return(expression);
        }