/// <summary> /// Find the first IQueryable expression. /// </summary> /// <param name="expression">The current expression</param> /// <returns>The iqueryable expression.</returns> public static Expression Find(Expression expression) { RootQueryableFinder finder = new RootQueryableFinder(); finder.Visit(expression); return(finder.root); }
/// <summary> /// Compiles the expression through the delegate. /// </summary> /// <param name="args">The arguments for the method to execute.</param> internal void Compile(params object[] args) { if (this.fnQuery == null) { // First identify the query provider being used Expression body = this.query.Body; ConstantExpression root = RootQueryableFinder.Find(body) as ConstantExpression; if (root == null && args != null && args.Length > 0) { Expression replaced = ExpressionReplacer.ReplaceAll( body, this.query.Parameters.ToArray(), args.Select((a, i) => Expression.Constant(a, this.query.Parameters[i].Type)).ToArray() ); body = PartialEvaluator.Eval(replaced); root = RootQueryableFinder.Find(body) as ConstantExpression; } if (root == null) { throw new InvalidOperationException("Could not find query provider"); } // Ask the query provider to compile the query by 'executing' the lambda expression IQueryProvider provider = ((IQueryable)root.Value).Provider; Delegate result = (Delegate)provider.Execute(this.query); System.Threading.Interlocked.CompareExchange(ref this.fnQuery, result, null); } }