Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
fixed issue 19 in a roundabout way.
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson authored and lanwin committed Nov 8, 2010
1 parent 2802698 commit de04140
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
46 changes: 46 additions & 0 deletions source/MongoDB.Tests/Issues/Issue19.cs
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using MongoDB.Attributes;
using MongoDB.Configuration;

namespace MongoDB.Issues
{
[TestFixture]
public class Issue19
{
private class Post
{
[MongoId]
public Guid Id { get; set; }

public DateTime LastAccess { get; set; }
public List<string> Tags { get; set; }
}

[Test]
public void Should_return_an_empty_list()
{
var SearchTags = new List<string> { "test" };
using (var mongo = new Mongo())
{
mongo.Connect();
var db = mongo.GetDatabase("posts");
var posts = db.GetCollection<Post>();
posts.Remove(x => true);
posts.Insert(new Post() { Tags = new List<string> { "test", "funny" } });

var result = SearchTags
.Aggregate(posts.Linq(), (current, tag) => current.Where(x => x.Tags.Contains(tag)))
.OrderByDescending(x => x.LastAccess)
.ToList();

Assert.AreEqual(1, result.Count);
}
}

}
}
1 change: 1 addition & 0 deletions source/MongoDB.Tests/MongoDB.Tests.csproj
Expand Up @@ -94,6 +94,7 @@
<Compile Include="IntegrationTests\SecondServer\TestPooledConnectionFactory.cs" />
<Compile Include="IntegrationTests\Linq\LinqDomain.cs" />
<Compile Include="IntegrationTests\Linq\MapReduceTests.cs" />
<Compile Include="Issues\Issue19.cs" />
<Compile Include="UnitTests\Bson\BsonTestBase.cs" />
<Compile Include="UnitTests\Bson\TestBsonBinary.cs" />
<Compile Include="UnitTests\Bson\TestRoundTrips.cs" />
Expand Down
24 changes: 18 additions & 6 deletions source/MongoDB/Linq/Translators/DocumentFormatter.cs
Expand Up @@ -183,12 +183,24 @@ protected override Expression VisitMethodCall(MethodCallExpression m)
{
case "Contains":
field = m.Arguments[0] as FieldExpression;
if (field == null)
throw new InvalidQueryException(string.Format("The mongo field must be the argument in method {0}.", m.Method.Name));
VisitPredicate(field, true);
AddCondition("$in", EvaluateConstant<IEnumerable>(m.Object).OfType<object>().ToArray());
PopConditionScope();
return m;
if (field != null)
{
VisitPredicate(field, true);
AddCondition("$in", EvaluateConstant<IEnumerable>(m.Object).OfType<object>().ToArray());
PopConditionScope();
return m;
}

field = m.Object as FieldExpression;
if (field != null)
{
VisitPredicate(m.Object, true);
AddCondition(EvaluateConstant<object>(m.Arguments[0]));
PopConditionScope();
return m;
}

throw new InvalidQueryException(string.Format("The mongo field must be the argument in method {0}.", m.Method.Name));
}
}
else if (m.Method.DeclaringType == typeof(string))
Expand Down

0 comments on commit de04140

Please sign in to comment.