// Executes the expression tree that is passed to it. public 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."); } string whereClause = ""; // Find the call to Where() and get the lambda expression predicate. var whereExpression = new InnermostWhereFinder().GetInnermostWhere(expression); if (whereExpression != null) { var lambdaExpression = (LambdaExpression)((UnaryExpression)whereExpression.Arguments[1]).Operand; // Send the lambda expression through the partial evaluator. lambdaExpression = (LambdaExpression)Evaluator.PartialEval(lambdaExpression); var builder = new FSWhereBuilder(lambdaExpression.Body); whereClause = builder.WhereClause; } var queryableResult = _fsRep.Get(whereClause).AsQueryable(); return(isEnumerable ? queryableResult.Provider.CreateQuery(expression) : queryableResult.Provider.Execute(expression)); }
private bool ItemShouldBeInList(T item) { InnermostWhereFinder whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(_expression); if (whereExpression == null) { return(false); } if (whereExpression.Arguments.Count < 2) { return(false); } if (whereExpression.Arguments[1] == null) { return(false); } Expression <Func <T, bool> > whereBody = (Expression <Func <T, bool> >)((UnaryExpression)(whereExpression.Arguments[1])).Operand; var searchable = _list as Linq.IIndexSearchable <T>; if (searchable == null) { return(false); } else { return(searchable.SearchByExpression(whereBody).Contains(item)); } }
// 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)); }
private void SetFilterByExpression(Expression expression) { InnermostWhereFinder whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(expression); if (whereExpression == null) { return; } if (whereExpression.Arguments.Count < 2) { return; } if (whereExpression.Arguments[1] == null) { return; } Expression <Func <T, bool> > whereBody = (Expression <Func <T, bool> >)((UnaryExpression)(whereExpression.Arguments[1])).Operand; if (whereBody.Body is BinaryExpression) { BinaryExpression binExp = (BinaryExpression)whereBody.Body; if (binExp.Left.NodeType == ExpressionType.MemberAccess && (binExp.Left as MemberExpression).Member.MemberType == MemberTypes.Property) { _filterBy = TypeDescriptor.GetProperties(typeof(T))[(binExp.Left as MemberExpression).Member.Name]; } } }
// 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); }
internal void BuildFilterIndex() { // Find the call to Where() and get the lambda expression predicate. InnermostWhereFinder whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(_expression); if (whereExpression == null) { return; } if (whereExpression.Arguments.Count < 2) { return; } if (whereExpression.Arguments[1] == null) { return; } Expression <Func <T, bool> > whereBody = (Expression <Func <T, bool> >)((UnaryExpression)(whereExpression.Arguments[1])).Operand; _filterIndex.Clear(); if (_list is Linq.IIndexSearchable <T> ) { //before we can start, we do have to go through the whole thing once to make our filterindex. foreach (T item in (_list as Linq.IIndexSearchable <T>).SearchByExpression(whereBody)) { if (_filterBy != null) { object tmp = _filterBy.GetValue(item); _filterIndex.Add(new ListItem(tmp, ((IPositionMappable <T>)_list).PositionOf(item))); } else { _filterIndex.Add(new ListItem(item.GetHashCode(), ((IPositionMappable <T>)_list).PositionOf(item))); } } } //else // foreach (T item in _list.Where((Func<T, bool>) (whereBody as LambdaExpression).Compile())) // object tmp = _filterBy.GetValue(item); // _filterIndex.Add(new ListItem(tmp, ((IPositionMappable<T>)_list).PositionOf(item))); // } }
/// <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)); }
/// <summary> /// Gets an enumerator object. /// </summary> /// <returns></returns> public IEnumerator <T> GetEnumerator() { if (_list is Linq.IIndexSearchable <T> && _list is Core.IPositionMappable <T> ) { // Find the call to Where() and get the lambda expression predicate. InnermostWhereFinder whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(_expression); Expression <Func <T, bool> > whereBody = (Expression <Func <T, bool> >)((UnaryExpression)(whereExpression.Arguments[1])).Operand; foreach (T item in (_list as Linq.IIndexSearchable <T>).SearchByExpression(whereBody)) { yield return(item); } } else { foreach (T item in _list) { yield return(item); } } }
/// <summary> /// Executes the expression tree that is passed to it. /// </summary> static internal 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 whereExpression = InnermostWhereFinder.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 LocationFinder(lambdaExpression.Body); * var locations = lf.Locations; * 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. * Place[] places = WebServiceHelper.GetPlacesFromTerraServer(locations); * * // Copy the IEnumerable places to an IQueryable. * IQueryable<Place> queryablePlaces = places.AsQueryable(); * * var newExpressionTree = CopyTree(expression, queryablePlaces); * * // 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); */ // TODO: temp return(null); }
/// <summary> /// Used to derive the lambda expression and determine whether the query is correct /// </summary> private void ExtrapolateLambdas() { var methodCallExpression = _expression as MethodCallExpression; if (methodCallExpression == null && methodCallExpression.Arguments.Count == 0) { throw new InvalidQueryException("unable to execute query ensure that expression is a Linq expression"); } var argumentExpression = methodCallExpression.Arguments[0] as MethodCallExpression; if (argumentExpression == null && argumentExpression.Arguments.Count == 0) { throw new InvalidQueryException("unable to execute query ensure that expression is a Linq expression"); } // get the first argument of the expression which should be focussed on the initial var typeExpression = argumentExpression.Arguments[0] as ConstantExpression; if (typeExpression == null) { throw new InvalidQueryException("unable to execute query ensure that expression is a Linq expression"); } _expressionQueryableType = typeExpression.Type; var whereFinder = new InnermostWhereFinder(); MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(_expression); // if we don't have a where clause we only want to evaluate the first part of the expression if (whereExpression == null) { return; } var lambdaExpression = (LambdaExpression)((UnaryExpression)(whereExpression.Arguments[1])).Operand; // Send the lambda expression through the partial evaluator. _lambdaExpression = (LambdaExpression)Evaluator.PartialEval(lambdaExpression); }