Skip to content

Commit

Permalink
support for Any in Linq Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed Jul 9, 2010
1 parent b442b0c commit ab62f7e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
8 changes: 8 additions & 0 deletions source/MongoDB.Tests/IntegrationTests/Linq/MongoQueryTests.cs
Expand Up @@ -63,6 +63,14 @@ public override void TestSetup()
true);
}

[Test]
public void Any()
{
var anyone = Collection.Linq().Any(x => x.Age <= 21);

Assert.IsTrue(anyone);
}

[Test]
public void Boolean()
{
Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB/Linq/MongoQueryProvider.cs
Expand Up @@ -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;
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Linq/Translators/MongoQueryObjectBuilder.cs
Expand Up @@ -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);
Expand Down
26 changes: 18 additions & 8 deletions source/MongoDB/Linq/Translators/QueryBinder.cs
Expand Up @@ -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":
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ab62f7e

Please sign in to comment.