Skip to content

Commit e0634a9

Browse files
author
rstam
committed
Removed partially implemented support for Enumerable.Any with predicate. It wasn't using $elemMatch and full support requires lots more development and testing and most likely support in the server for new forms of $elemMatch (for example support $and and $or inside an $elemMatch). This will be reimplemented in a future release. See CSHARP-413.
1 parent 4592912 commit e0634a9

File tree

3 files changed

+5
-51
lines changed

3 files changed

+5
-51
lines changed

Driver/Linq/LinqToMongo.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ namespace MongoDB.Driver.Linq
3030
/// </summary>
3131
public static class LinqToMongo
3232
{
33-
/// <summary>
34-
/// Represents an arbitrary item in an array value. Can only be used in LINQ queries.
35-
/// </summary>
36-
/// <param name="source">The array value.</param>
37-
/// <returns>Throws an InvalidOperationException if called.</returns>
38-
public static TSource Arbitrary<TSource>(this IEnumerable<TSource> source)
39-
{
40-
throw new InvalidOperationException("The LinqToMongo.Arbitrary method is only intended to be used in LINQ Where clauses.");
41-
}
42-
4333
/// <summary>
4434
/// Determines whether a sequence contains all of the specified values.
4535
/// </summary>

Driver/Linq/Translators/SelectQuery.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,7 @@ private IMongoQuery BuildAnyQuery(MethodCallExpression methodCallExpression)
289289
}
290290
else if (arguments.Length == 2)
291291
{
292-
var sourceExpression = arguments[0];
293-
var lambda = (LambdaExpression)arguments[1];
294-
var parameter = lambda.Parameters[0];
295-
var body = lambda.Body;
296-
var arbitraryMethodInfo = typeof(LinqToMongo).GetMethod("Arbitrary").MakeGenericMethod(parameter.Type);
297-
var arbitraryMethodCallExpression = Expression.Call(arbitraryMethodInfo, sourceExpression);
298-
var modifiedBody = ExpressionParameterReplacer.ReplaceParameter(body, parameter, arbitraryMethodCallExpression);
299-
return BuildQuery(modifiedBody);
292+
throw new NotSupportedException("Enumerable.Any with a predicate is not supported.");
300293
}
301294
}
302295
return null;
@@ -919,25 +912,9 @@ private BsonSerializationInfo GetSerializationInfo(IBsonSerializer serializer, E
919912
}
920913

921914
var methodCallExpression = expression as MethodCallExpression;
922-
if (methodCallExpression != null)
915+
if (methodCallExpression != null && methodCallExpression.Method.Name == "get_Item")
923916
{
924-
switch (methodCallExpression.Method.Name)
925-
{
926-
case "Arbitrary":
927-
if (methodCallExpression.Method.DeclaringType == typeof(LinqToMongo))
928-
{
929-
var arraySerializationInfo = GetSerializationInfo(serializer, methodCallExpression.Arguments[0]);
930-
var itemSerializationInfo = arraySerializationInfo.Serializer.GetItemSerializationInfo();
931-
return new BsonSerializationInfo(
932-
arraySerializationInfo.ElementName,
933-
itemSerializationInfo.Serializer,
934-
itemSerializationInfo.NominalType,
935-
itemSerializationInfo.SerializationOptions);
936-
}
937-
break;
938-
case "get_Item":
939-
return GetSerializationInfoGetItem(serializer, methodCallExpression);
940-
}
917+
return GetSerializationInfoGetItem(serializer, methodCallExpression);
941918
}
942919

943920
return null;

DriverOnlineTests/Linq/SelectQueryTests.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,26 +1926,13 @@ where c.A.Any()
19261926
}
19271927

19281928
[Test]
1929+
[ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Enumerable.Any with a predicate is not supported.")]
19291930
public void TestWhereAAnyWithPredicate()
19301931
{
19311932
var query = from c in _collection.AsQueryable<C>()
19321933
where c.A.Any(a => a > 3)
19331934
select c;
1934-
1935-
var translatedQuery = MongoQueryTranslator.Translate(query);
1936-
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
1937-
Assert.AreSame(_collection, translatedQuery.Collection);
1938-
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
1939-
1940-
var selectQuery = (SelectQuery)translatedQuery;
1941-
Assert.AreEqual("(C c) => Enumerable.Any<Int32>(c.A, (Int32 a) => (a > 3))", ExpressionFormatter.ToString(selectQuery.Where));
1942-
Assert.IsNull(selectQuery.OrderBy);
1943-
Assert.IsNull(selectQuery.Projection);
1944-
Assert.IsNull(selectQuery.Skip);
1945-
Assert.IsNull(selectQuery.Take);
1946-
1947-
Assert.AreEqual("{ \"a\" : { \"$gt\" : 3 } }", selectQuery.BuildQuery().ToJson());
1948-
Assert.AreEqual(1, Consume(query));
1935+
query.ToList(); // execute query
19491936
}
19501937

19511938
[Test]

0 commit comments

Comments
 (0)