-
Type: Bug
-
Resolution: Duplicate
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Dotnet Drivers
Summary
Using the "is" operator in a LINQ query throws an error after upgrading the MongoDB.Driver library to version 3.0.
I am using a local standalone server running MongoDB 6.0.6 Community.
How to Reproduce
The following code produces the error:
var client = new MongoClient(connectionString); BsonClassMap.RegisterClassMap<Animal>(map => { map.AutoMap(); map.AddKnownType(typeof(Dog)); map.AddKnownType(typeof(Cat)); }); var collection = client.GetDatabase("test").GetCollection<Animal>("animals"); collection.InsertMany(new Animal[] { new Dog(), new Dog(), new Cat() }); var dogs = collection.AsQueryable().Where(animal => animal is Dog).ToList(); Debug.Assert(dogs.Count == 2); public class Animal { public ObjectId Id { get; set; } } public class Dog : Animal { } public class Cat : Animal { }
Running this code in version 2.30 of the MongoDB.Driver works as expected with no error.
Running the code in version 3.0 produces this error:
MongoDB.Driver.Linq.ExpressionNotSupportedException HResult=0x80131515 Message=Expression not supported: (animal Is Dog) because is operator is not supported with the configured discriminator convention. Source=MongoDB.Driver StackTrace: at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.TypeIsExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, TypeBinaryExpression expression) at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateUsingAggregationOperators(TranslationContext context, Expression expression) at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.Translate(TranslationContext context, Expression expression, Boolean exprOk) at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateLambda(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot) at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.WhereMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression) at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression, ExpressionTranslationOptions translationOptions) at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute() at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
- duplicates
-
CSHARP-5356 Support `OfType` and `is` with scalar discriminators
- Backlog