Skip to content

Commit fd4bdff

Browse files
committed
CSHARP-732: fixed linq predicate not working with types derived from ICollection<T>
1 parent 619e564 commit fd4bdff

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

MongoDB.Driver/Linq/Translators/PredicateTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ private IMongoQuery BuildInQuery(MethodCallExpression methodCallExpression)
627627
methodDeclaringType = methodDeclaringType.GetGenericTypeDefinition();
628628
}
629629

630-
bool contains = methodDeclaringType.GetInterface("ICollection`1") != null;
630+
bool contains = methodDeclaringType == typeof(ICollection<>) || methodDeclaringType.GetInterface("ICollection`1") != null;
631631
if (contains && arguments.Length == 1)
632632
{
633633
serializationInfo = _serializationInfoHelper.GetSerializationInfo(arguments[0]);

MongoDB.DriverUnitTests/Linq/SelectQueryTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,32 @@ where local.Contains(c.X)
20652065
Assert.AreEqual(3, Consume(query));
20662066
}
20672067

2068+
[Test]
2069+
public void TestWhereLocalIListContainsX()
2070+
{
2071+
// this will generate a non-list, non-array.
2072+
IList<int> local = new[] { 1, 2, 3 };
2073+
2074+
var query = from c in _collection.AsQueryable<C>()
2075+
where local.Contains(c.X)
2076+
select c;
2077+
2078+
var translatedQuery = MongoQueryTranslator.Translate(query);
2079+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
2080+
Assert.AreSame(_collection, translatedQuery.Collection);
2081+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
2082+
2083+
var selectQuery = (SelectQuery)translatedQuery;
2084+
Assert.AreEqual("(C c) => Int32[]:{ 1, 2, 3 }.Contains(c.X)", ExpressionFormatter.ToString(selectQuery.Where));
2085+
Assert.IsNull(selectQuery.OrderBy);
2086+
Assert.IsNull(selectQuery.Projection);
2087+
Assert.IsNull(selectQuery.Skip);
2088+
Assert.IsNull(selectQuery.Take);
2089+
2090+
Assert.AreEqual("{ \"x\" : { \"$in\" : [1, 2, 3] } }", selectQuery.BuildQuery().ToJson());
2091+
Assert.AreEqual(3, Consume(query));
2092+
}
2093+
20682094
[Test]
20692095
public void TestWhereAContains2()
20702096
{

0 commit comments

Comments
 (0)