Skip to content

Commit

Permalink
CSHARP-1431: fixed issue with a local Contains operation inside an An…
Browse files Browse the repository at this point in the history
…y predicate.
  • Loading branch information
craiggwilson committed Oct 2, 2015
1 parent e5d8f60 commit f55eecc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public void Any_with_a_predicate_on_scalars()
// })));
}

[Test]
public void Any_with_local_contains()
{
var local = new List<string> { "Delilah", "Dolphin" };

Assert(
x => x.G.Any(g => local.Contains(g.D)),
1,
"{\"G.D\": { $in: [\"Delilah\", \"Dolphin\" ] } }");
}

[Test]
public void LocalIListContains()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,18 @@ protected internal virtual Expression VisitPipeline(PipelineExpression node)
{
return node.Update(
Visit(node.Source),
Visit(node.Projector));
Visit(node.Projector),
VisitResultOperator(node.ResultOperator));
}

protected internal virtual ResultOperator VisitResultOperator(ResultOperator resultOperator)
{
if (resultOperator == null)
{
return resultOperator;
}

return resultOperator.Update(this);
}

protected internal virtual Expression VisitSelect(SelectExpression node)
Expand Down
7 changes: 4 additions & 3 deletions src/MongoDB.Driver/Linq/Expressions/PipelineExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ public override string ToString()
return result;
}

public PipelineExpression Update(Expression source, Expression projector)
public PipelineExpression Update(Expression source, Expression projector, ResultOperator resultOperator)
{
if (source != _source ||
projector != _projector)
projector != _projector ||
resultOperator != _resultOperator)
{
return new PipelineExpression(source, projector, _resultOperator);
return new PipelineExpression(source, projector, resultOperator);
}

return this;
Expand Down
5 changes: 5 additions & 0 deletions src/MongoDB.Driver/Linq/Expressions/ResultOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ internal abstract class ResultOperator
public abstract string Name { get; }

public abstract Type Type { get; }

protected internal virtual ResultOperator Update(ExtensionExpressionVisitor visitor)
{
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,16 @@ public Expression Value
{
get { return _value; }
}

protected internal override ResultOperator Update(ExtensionExpressionVisitor visitor)
{
var value = visitor.Visit(_value);
if (value != _value)
{
return new ContainsResultOperator(value);
}

return this;
}
}
}
9 changes: 6 additions & 3 deletions src/MongoDB.Driver/Linq/Translators/FieldNamePrefixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected internal override Expression VisitField(FieldExpression node)

protected internal override Expression VisitPipeline(PipelineExpression node)
{
// only do the root source of this one...
var source = node.Source;
var sourcedSource = source as ISourcedExpression;
while (sourcedSource != null)
Expand All @@ -60,12 +59,16 @@ protected internal override Expression VisitPipeline(PipelineExpression node)
}

var newSource = Visit(source);
PipelineExpression newNode = node;
if (newSource != source)
{
return ExpressionReplacer.Replace(node, source, newSource);
newNode = (PipelineExpression)ExpressionReplacer.Replace(node, source, newSource);
}

return node;
return newNode.Update(
newNode.Source,
newNode.Projector,
VisitResultOperator(newNode.ResultOperator));
}
}
}

0 comments on commit f55eecc

Please sign in to comment.