Exemple #1
0
            private IQueryProvider FindProvider(Expression expression, object[] args)
            {
                ConstantExpression root = TypedSubtreeFinder.Find(expression,
                                                                  typeof(IQueryProvider)) as ConstantExpression;

                if (root == null)
                {
                    root = TypedSubtreeFinder.Find(expression, typeof(IQueryable)) as ConstantExpression;
                }

                if (root == null && args != null && args.Length > 0)
                {
                    Expression replaced = ExpressionReplacer.ReplaceAll(
                        expression, _expression.Parameters.ToArray(),
                        args.Select((a, i) => Expression.Constant(a, _expression.Parameters[i].Type)).ToArray()
                        );
                    Expression partial = PartialEvaluator.Eval(replaced);
                    return(FindProvider(partial, null));
                }

                if (root != null)
                {
                    IQueryProvider provider = (root.Value as IQueryProvider);
                    if (provider == null)
                    {
                        IQueryable query = (root.Value as IQueryable);
                        if (query != null)
                        {
                            provider = query.Provider;
                        }
                    }
                    return(provider);
                }
                return(null);
            }
        /// <summary>
        /// Convert the query expression into an execution plan
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected virtual Expression GetExecutionPlan(Expression expression)
        {
            // strip off lambda for now
            LambdaExpression lambda = expression as LambdaExpression;

            if (lambda != null)
            {
                expression = lambda.Body;
            }

            // translate query into client & server parts
            Expression translation = this.Translate(expression);

            Expression provider = TypedSubtreeFinder.Find(expression, typeof(DbQueryProvider));

            if (provider == null)
            {
                Expression rootQueryable = TypedSubtreeFinder.Find(expression, typeof(IQueryable));
                provider = Expression.Convert(
                    Expression.Property(rootQueryable, typeof(IQueryable).GetProperty("Provider")),
                    typeof(DbQueryProvider)
                    );
            }

            return(this.policy.BuildExecutionPlan(this.Mapping, translation, provider));
        }
        public static Expression Find(Expression expression, Type type)
        {
            TypedSubtreeFinder finder = new TypedSubtreeFinder(type);
            finder.Visit(expression);

            return finder._root;
        }
        public static Expression Find(Expression expression, Type type)
        {
            TypedSubtreeFinder finder = new TypedSubtreeFinder(type);

            finder.Visit(expression);

            return(finder._root);
        }