private Expression MapAnyOrAllExpression(MethodCallExpression node) { if (_DataSpace != DataSpace.CSpace) { throw new ApplicationException("Filters on child collections are only supported when using CSpace"); } if ((node.Arguments == null) || (node.Arguments.Count > 2)) { throw new ApplicationException("Any function call has more than 2 arguments"); } // Visit the first argument so that we can get the DbPropertyExpression which is the source of the method call. var sourceExpression = Visit(node.Arguments[0]); var collectionExpression = GetDbExpressionForExpression(sourceExpression); // Visit this DbExpression using the QueryVisitor in case it has it's own filters that need to be applied. var queryVisitor = new DynamicFilterQueryVisitorCSpace(_DbContext); collectionExpression = collectionExpression.Accept(queryVisitor); DbExpression dbExpression; if (node.Arguments.Count == 2) { // The method call has a predicate that needs to be evaluated. This must be done against the source // argument - not the current binding. var binding = collectionExpression.Bind(); // Visit the lambda expression against this binding (which will evaluate all of the // conditions in the expression against this binding and for this filter). var lambdaExpression = node.Arguments[1] as LambdaExpression; var visitor = new LambdaToDbExpressionVisitor(_Filter, binding, _DbContext, _DataSpace); var subExpression = visitor.Visit(lambdaExpression) as LambdaExpression; var subDbExpression = visitor.GetDbExpressionForExpression(subExpression.Body); // Create an "Any" or "All" DbExpression from the results if (node.Method.Name == "All") { dbExpression = DbExpressionBuilder.All(binding, subDbExpression); } else { dbExpression = DbExpressionBuilder.Any(binding, subDbExpression); } } else { // This should not even be possible - linq/IEnumerable does not have such a method! if (node.Method.Name == "All") { throw new ApplicationException("All() with no parameters is not supported"); } // No predicate so just create an Any DbExpression against the collection expression dbExpression = DbExpressionBuilder.Any(collectionExpression); } MapExpressionToDbExpression(node, dbExpression); return(node); }