Permalink
Browse files

support for Any in Linq Provider

  • Loading branch information...
1 parent b442b0c commit ab62f7e21020e013357a3a72c9734f8030090dc0 @craiggwilson craiggwilson committed Jul 9, 2010
@@ -64,6 +64,14 @@ public override void TestSetup()
}
[Test]
+ public void Any()
+ {
+ var anyone = Collection.Linq().Any(x => x.Age <= 21);
+
+ Assert.IsTrue(anyone);
+ }
+
+ [Test]
public void Boolean()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => x.PrimaryAddress.IsInternational));
@@ -160,7 +160,7 @@ private Expression BuildExecutionPlan(Expression expression)
return new ExecutionBuilder().Build(projection, provider);
}
- private ProjectionExpression Translate(Expression expression)
+ private Expression Translate(Expression expression)
{
var rootQueryable = new RootQueryableFinder().Find(expression);
var elementType = ((IQueryable)((ConstantExpression)rootQueryable).Value).ElementType;
@@ -177,7 +177,7 @@ private ProjectionExpression Translate(Expression expression)
expression = new RedundantFieldRemover().Remove(expression);
expression = new RedundantSubqueryRemover().Remove(expression);
- return (ProjectionExpression)expression;
+ return expression;
}
/// <summary>
@@ -9,7 +9,7 @@ internal class MongoQueryObjectBuilder : MongoExpressionVisitor
private MongoQueryObject _queryObject;
private QueryAttributes _queryAttributes;
- internal MongoQueryObject Build(ProjectionExpression expression)
+ internal MongoQueryObject Build(Expression expression)
{
_queryObject = new MongoQueryObject();
_queryAttributes = new QueryAttributesGatherer().Gather(expression);
@@ -128,6 +128,11 @@ protected override Expression VisitMethodCall(MethodCallExpression m)
{
switch (m.Method.Name)
{
+ case "Any":
+ if (m.Arguments.Count == 1)
+ return BindAny(m.Arguments[0], null, m == _root);
+ else
+ return BindAny(m.Arguments[0], (LambdaExpression)StripQuotes(m.Arguments[1]), m == _root);
case "Where":
return BindWhere(m.Type, m.Arguments[0], (LambdaExpression)StripQuotes(m.Arguments[1]));
case "Select":
@@ -273,16 +278,21 @@ private Expression BindAggregate(Expression source, MethodInfo method, LambdaExp
return subquery;
}
- private Expression BindAny(Expression source, LambdaExpression predicate)
+ private Expression BindAny(Expression source, LambdaExpression predicate, bool isRoot)
{
var projection = VisitSequence(source);
- _map[predicate.Parameters[0]] = projection.Projector;
- var where = Visit(predicate.Body);
- var alias = new Alias();
- var fieldProjection = _projector.ProjectFields(projection.Projector, alias, projection.Source.Alias);
- return new ProjectionExpression(
- new SelectExpression(alias, fieldProjection.Fields, projection.Source, where),
- fieldProjection.Projector);
+ var sourceType = projection.Projector.Type;
+
+ MethodInfo method = typeof(Queryable)
+ .GetMethods(BindingFlags.Public | BindingFlags.Static)
+ .Where(m => m.Name == "Count")
+ .Single(m => m.GetParameters().Length == (predicate == null ? 1 : 2))
+ .GetGenericMethodDefinition().MakeGenericMethod(sourceType);
+
+ var expression = BindAggregate(source, method, predicate, isRoot);
+
+ return Expression.GreaterThan(
+ expression, Expression.Constant(0));
}
private Expression BindDistinct(Expression source)

0 comments on commit ab62f7e

Please sign in to comment.