// Executes the expression tree that is passed to it. internal static object Execute(Expression expression, bool isEnumerable) { // The expression must represent a query over the data source. if (!IsQueryOverDataSource(expression)) { throw new InvalidProgramException("No query over the data source was specified."); } // Find the call to Where() and get the lambda expression predicate. InnermostWhereFinder whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(expression); LambdaExpression lambdaExpression = (LambdaExpression)((UnaryExpression)(whereExpression.Arguments[1])).Operand; // Send the lambda expression through the partial evaluator. lambdaExpression = (LambdaExpression)Evaluator.PartialEval(lambdaExpression); UserFinder uf = new UserFinder(lambdaExpression.Body); List <string> names = uf.Usernames; if (names.Count == 0) { throw new InvalidQueryException("You must specify atleast one name for this query."); } IQueryable <UserSession> queryableSessions = sessions.AsQueryable <UserSession>(); // Copy the expression tree that was passed in, changing only the first // argument of the innermost MethodCallExpression. ExpressionTreeModifier treeCopier = new ExpressionTreeModifier(queryableSessions); Expression newExpressionTree = treeCopier.Visit(expression); // This step creates an IQueryable that executes by replacing Queryable methods with Enumerable methods. return(isEnumerable ? queryableSessions.Provider.CreateQuery(newExpressionTree) : queryableSessions.Provider.Execute(newExpressionTree)); }
// Executes the expression tree that is passed to it. internal static object Execute(Expression expression, bool IsEnumerable, VismaNetAuthorization auth) { // The expression must represent a query over the data source. if (!IsQueryOverDataSource(expression)) { throw new InvalidProgramException("No query over the data source was specified."); } // Find the call to Where() and get the lambda expression predicate. InnermostWhereFinder whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(expression); var orderBy = GetOrderByExpression(expression); var numberToRead = GetTakeExpression(expression); LambdaExpression lambdaExpression; IEnumerable <Customer> customers; if (whereExpression != null && whereExpression.Arguments.Count > 1) { lambdaExpression = (LambdaExpression)((UnaryExpression)(whereExpression.Arguments[1])).Operand; // Send the lambda expression through the partial evaluator. lambdaExpression = (LambdaExpression)Evaluator.PartialEval(lambdaExpression); // Get the place name(s) to query the Web service with. var cf = new CustomerFinder(lambdaExpression.Body); if (whereExpression.Method.Name.StartsWith("First")) { numberToRead = 1; } // Call the Web service and get the results. customers = VismaNetApiHelper.FindCustomers(cf.UrlParams, auth, orderBy: orderBy, numberToRead: numberToRead); } else { customers = VismaNetApiHelper.FindCustomers(null, auth, orderBy: orderBy, numberToRead: numberToRead); // Return all customers } // Copy the IEnumerable places to an IQueryable. IQueryable <Customer> queryableCustomers = customers.AsQueryable <Customer>(); // Copy the expression tree that was passed in, changing only the first // argument of the innermost MethodCallExpression. var treeCopier = new ExpressionTreeModifier <Customer>(queryableCustomers); Expression newExpressionTree = treeCopier.Visit(expression); // // This step creates an IQueryable that executes by replacing Queryable methods with Enumerable methods. // if (IsEnumerable) return(queryableCustomers.Provider.CreateQuery(newExpressionTree)); // else // return queryableCustomers.Provider.Execute(newExpressionTree); }
public static object ModifyExpression(Expression expression, IQueryable <Issue> issues, bool isEnumerable) { if (isEnumerable) { return(issues); } else { var treeCopier = new ExpressionTreeModifier(issues); Expression newExpressionTree = treeCopier.Visit(expression); return(issues.Provider.Execute(newExpressionTree)); } }
/// <summary> /// Executes the expression tree that is passed to it. /// </summary> /// <param name="expression">Expression Tree</param> /// <param name="isEnumerable"></param> /// <returns></returns> internal static object Execute(Expression expression, bool isEnumerable) { // The expression must represent a query over the data source. if (!IsQueryOverDataSource(expression)) { throw new InvalidProgramException("No query over the data source was specified."); } // Find the call to Where() and get the lambda expression predicate. var whereFinder = new InnermostWhereFinder(); var whereExpression = whereFinder.GetInnermostWhere(expression); var lambdaExpression = (LambdaExpression)((UnaryExpression)(whereExpression.Arguments[1])).Operand; // Send the lambda expression through the partial evaluator. lambdaExpression = (LambdaExpression)Evaluator.PartialEval(lambdaExpression); // Get the place name(s) to query the Web service with. var lf = new TrackFinder(lambdaExpression.Body); var locations = lf.TrackNames; if (locations.Count == 0) { throw new InvalidQueryException("You must specify at least one place name in your query."); } // Call the Web service and get the results. Track[] places = WebServiceHelper.GetTracks(locations); // Copy the IEnumerable places to an IQueryable. IQueryable <Track> queryablePlaces = places.AsQueryable(); // Copy the expression tree that was passed in, changing only the first // argument of the innermost MethodCallExpression. var treeCopier = new ExpressionTreeModifier(queryablePlaces); var newExpressionTree = treeCopier.Visit(expression); // This step creates an IQueryable that executes by replacing Queryable methods with Enumerable methods. if (isEnumerable) { return(queryablePlaces.Provider.CreateQuery(newExpressionTree)); } return(queryablePlaces.Provider.Execute(newExpressionTree)); }