Ejemplo n.º 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");
            }
            IEntityProvider            ep = provider as IEntityProvider;
            Func <Expression, bool>    fnCanBeEvaluated = (ep != null) ? new Func <Expression, bool>(ep.CanBeEvaluatedLocally) : null;
            List <ParameterExpression> parameters       = new List <ParameterExpression>();
            List <object> values     = new List <object>();
            Expression    expression = PartialEvaluator.Eval(query, fnCanBeEvaluated, delegate(ConstantExpression c) {
                bool flag = c.Value is IQueryable;
                if (!((flag || (ep == null)) || ep.CanBeParameter(c)))
                {
                    return(c);
                }
                ParameterExpression item = Expression.Parameter(c.Type, "p" + parameters.Count);
                parameters.Add(item);
                values.Add(c.Value);
                if (flag)
                {
                    return(c);
                }
                return(item);
            });

            if (expression.Type != typeof(object))
            {
                expression = Expression.Convert(expression, typeof(object));
            }
            arguments = values.ToArray();
            if (arguments.Length < 5)
            {
                return(Expression.Lambda(expression, parameters.ToArray()));
            }
            arguments = new object[] { arguments };
            return(ExplicitToObjectArray.Rewrite(expression, parameters));
        }
Ejemplo n.º 2
0
            internal IQueryProvider FindProvider(Expression expression, object[] args)
            {
                Func <object, int, ConstantExpression> selector = null;
                Expression expression2 = this.FindProviderInExpression(expression) as ConstantExpression;

                if (((expression2 == null) && (args != null)) && (args.Length > 0))
                {
                    if (selector == null)
                    {
                        selector = (a, i) => Expression.Constant(a, this.query.Parameters[i].Type);
                    }
                    Expression expression3 = ExpressionReplacer.ReplaceAll(expression, this.query.Parameters.ToArray <ParameterExpression>(), args.Select <object, ConstantExpression>(selector).ToArray <ConstantExpression>());
                    expression2 = this.FindProviderInExpression(expression3);
                }
                if (expression2 != null)
                {
                    ConstantExpression expression4 = expression2 as ConstantExpression;
                    if (expression4 == null)
                    {
                        expression4 = PartialEvaluator.Eval(expression2) as ConstantExpression;
                    }
                    if (expression4 != null)
                    {
                        IQueryProvider provider = expression4.Value as IQueryProvider;
                        if (provider == null)
                        {
                            IQueryable queryable = expression4.Value as IQueryable;
                            if (queryable != null)
                            {
                                provider = queryable.Provider;
                            }
                        }
                        return(provider);
                    }
                }
                return(null);
            }