Beispiel #1
0
        private LambdaExpression Parameterize(Expression query, out object[] arguments)
        {
            IQueryProvider provider = this.FindProvider(query);

            if (provider == null)
            {
                throw new ArgumentException("Cannot deduce query provider from query");
            }

            var ep = provider as IEntityProvider;
            Func <Expression, bool>    fn         = ep != null ? (Func <Expression, bool>)ep.CanBeEvaluatedLocally : null;
            List <ParameterExpression> parameters = new List <ParameterExpression>();
            List <object> values = new List <object>();

            var body = PartialEvaluator.Eval(query, fn, c =>
            {
                bool isQueryRoot = c.Value is IQueryable;
                if (!isQueryRoot && ep != null && !ep.CanBeParameter(c))
                {
                    return(c);
                }
                var p = Expression.Parameter(c.Type, "p" + parameters.Count);
                parameters.Add(p);
                values.Add(c.Value);
                // if query root then parameterize but don't replace in the tree
                if (isQueryRoot)
                {
                    return(c);
                }
                return(p);
            });

            if (body.Type != typeof(object))
            {
                body = Expression.Convert(body, typeof(object));
            }

            arguments = values.ToArray();
            if (arguments.Length < 5)
            {
                return(Expression.Lambda(body, parameters.ToArray()));
            }
            else
            {
                arguments = new object[] { arguments };
                return(ParameterToObjectArrayAccessorRewriter.Rewrite(body, parameters));
            }
        }
Beispiel #2
0
            internal IQueryProvider FindProvider(Expression expression, object[] args)
            {
                Expression root = this.FindProviderInExpression(expression) as ConstantExpression;

                if (root == null && args != null && args.Length > 0)
                {
                    Expression replaced = ExpressionReplacer.ReplaceAll(
                        expression,
                        this.query.Parameters.ToArray(),
                        args.Select((a, i) => Expression.Constant(a, this.query.Parameters[i].Type)).ToArray()
                        );
                    root = this.FindProviderInExpression(replaced);
                }
                if (root != null)
                {
                    ConstantExpression cex = root as ConstantExpression;
                    if (cex == null)
                    {
                        cex = PartialEvaluator.Eval(root) as ConstantExpression;
                    }
                    if (cex != null)
                    {
                        IQueryProvider provider = cex.Value as IQueryProvider;
                        if (provider == null)
                        {
                            IQueryable query = cex.Value as IQueryable;
                            if (query != null)
                            {
                                provider = query.Provider;
                            }
                        }
                        return(provider);
                    }
                }
                return(null);
            }