Пример #1
0
        public static IQueryable OrderBy(this IQueryable source, string ordering, QueryResolver queryResolver)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (ordering == null)
            {
                throw new ArgumentNullException("ordering");
            }

            ParameterExpression[] parameters = new ParameterExpression[] 
            {
                Expression.Parameter(source.ElementType, "") 
            };
            ExpressionParser parser = new ExpressionParser(parameters, ordering, queryResolver);
            IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
            Expression queryExpr = source.Expression;
            string methodAsc = "OrderBy";
            string methodDesc = "OrderByDescending";
            foreach (DynamicOrdering o in orderings)
            {
                queryExpr = Expression.Call(
                    typeof(Queryable),
                    o.Ascending ? methodAsc : methodDesc,
                    new Type[] { source.ElementType, o.Selector.Type },
                    queryExpr,
                    Expression.Quote(DynamicExpression.Lambda(o.Selector, parameters)));
                methodAsc = "ThenBy";
                methodDesc = "ThenByDescending";
            }
            return source.Provider.CreateQuery(queryExpr);
        }
Пример #2
0
        public static IQueryable Where(this IQueryable source, string predicate, QueryResolver queryResolver)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, queryResolver);

            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable),
                    "Where",
                    new Type[] { source.ElementType },
                    source.Expression,
                    Expression.Quote(lambda)));
        }
        internal static IQueryable Deserialize(IQueryable query, IEnumerable <ServiceQueryPart> queryParts, QueryResolver queryResolver)
        {
            if (query == null)
            {
                throw Error.ArgumentNull("query");
            }

            if (queryParts == null)
            {
                throw Error.ArgumentNull("queryParts");
            }

            foreach (ServiceQueryPart part in queryParts)
            {
                switch (part.QueryOperator)
                {
                case "filter":
                    try
                    {
                        query = DynamicQueryable.Where(query, part.Expression, queryResolver);
                    }
                    catch (ParseException e)
                    {
                        throw new ParseException(
                                  Error.Format(SRResources.ParseErrorInClause, "$filter", e.Message));
                    }
                    break;

                case "orderby":
                    try
                    {
                        query = DynamicQueryable.OrderBy(query, part.Expression, queryResolver);
                    }
                    catch (ParseException e)
                    {
                        throw new ParseException(
                                  Error.Format(SRResources.ParseErrorInClause, "$orderby", e.Message));
                    }
                    break;

                case "skip":
                    try
                    {
                        int skipCount = Convert.ToInt32(part.Expression, System.Globalization.CultureInfo.InvariantCulture);
                        if (skipCount < 0)
                        {
                            throw new ParseException(
                                      Error.Format(SRResources.PositiveIntegerExpectedForODataQueryParameter, "$skip", part.Expression));
                        }

                        query = DynamicQueryable.Skip(query, skipCount);
                    }
                    catch (FormatException e)
                    {
                        throw new ParseException(
                                  Error.Format(SRResources.ParseErrorInClause, "$skip", e.Message));
                    }
                    break;

                case "top":
                    try
                    {
                        int topCount = Convert.ToInt32(part.Expression, System.Globalization.CultureInfo.InvariantCulture);
                        if (topCount < 0)
                        {
                            throw new ParseException(
                                      Error.Format(SRResources.PositiveIntegerExpectedForODataQueryParameter, "$top", part.Expression));
                        }

                        query = DynamicQueryable.Take(query, topCount);
                    }
                    catch (FormatException e)
                    {
                        throw new ParseException(
                                  Error.Format(SRResources.ParseErrorInClause, "$top", e.Message));
                    }
                    break;
                }
            }

            return(query);
        }
Пример #4
0
            public ExpressionParser(ParameterExpression[] parameters, string expression, QueryResolver queryResolver)
            {
                if (expression == null)
                {
                    throw new ArgumentNullException("expression");
                }

                if (_keywords == null)
                {
                    _keywords = CreateKeywords();
                }

                this._queryResolver = queryResolver;
                _symbols = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
                _literals = new Dictionary<Expression, string>();

                if (parameters != null)
                {
                    ProcessParameters(parameters);
                }

                _text = expression;
                _textLen = _text.Length;
                SetTextPos(0);
                NextToken();
            }
Пример #5
0
 public static LambdaExpression ParseLambda(ParameterExpression[] parameters, Type resultType, string expression, QueryResolver queryResolver)
 {
     ExpressionParser parser = new ExpressionParser(parameters, expression, queryResolver);
     return Lambda(parser.Parse(resultType), parameters);
 }
Пример #6
0
 public static LambdaExpression ParseLambda(Type itType, Type resultType, string expression, QueryResolver queryResolver)
 {
     return ParseLambda(new ParameterExpression[] { Expression.Parameter(itType, "") }, resultType, expression, queryResolver);
 }