-
Type: Bug
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: 2.23.1
-
Component/s: None
-
None
-
Not Needed
-
Summary
The latest MongoDB.Driver for C# switched the default LINQ Provider from 2 to 3. After trying to upgrade and use version 3 the code that used to work in V2 fails in V3.
Server versions: 5.0.13 Windows and 5.0.10 Ubuntu
How to Reproduce
internal class Program7 internal class Program7 { public class Car { public string LicensePlate { get; set; } public CarType CarType { get; set; } } public enum CarType { Undefined = 0, Sport = 1, Suv = 2 } internal static void Main2(string[] args) { foreach (var linqProvider in new [] { LinqProvider.V2, LinqProvider.V3 }) { var builder = new MongoUrlBuilder { DatabaseName = "Sample", Server = new MongoServerAddress("localhost", 27017) }; var mongoClientSettings = MongoClientSettings.FromUrl(builder.ToMongoUrl()); mongoClientSettings.LinqProvider = linqProvider; var mongoClient = new MongoClient(mongoClientSettings); var mongoDatabase = mongoClient.GetDatabase(builder.DatabaseName); var carsCollections = mongoDatabase.GetCollection<Car>("Cars"); carsCollections.DeleteMany(c => true); carsCollections.InsertOne(new Car { LicensePlate = "CAL-" + Guid.NewGuid().ToString().Substring(6), CarType = CarType.Sport }); var result = carsCollections.AsQueryable() .GroupBy(c => 0) .Select(g => new { SportCarCount = g.Sum(c => c.CarType == CarType.Sport ? 1 : 0), SuvCarCount = g.Sum(c => c.CarType == CarType.Suv ? 1 : 0) }) .FirstOrDefault(); Console.WriteLine($"{linqProvider} - SportCarCount: {result.SportCarCount}"); Console.WriteLine($"{linqProvider} - SuvCarCount: {result.SuvCarCount}"); Console.WriteLine(); Console.WriteLine(); } } }
Execution output:
V2 - SportCarCount: 1
V2 - SuvCarCount: 0
Unhandled Exception: System.ArgumentException: Value type of serializer is Sample.Program7+CarType and does not match member type System.Int32.
Parameter name: serializer
at MongoDB.Bson.Serialization.BsonMemberMap.SetSerializer(IBsonSerializer serializer)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MemberInitExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression, NewExpression newExpression, IReadOnlyList`1 bindings)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.NewExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, NewExpression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.SelectMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.FirstMethodToExecutableQueryTranslator`1.Translate[TDocument](MongoQueryProvider`1 provider, TranslationContext context, MethodCallExpression expression)
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.TranslateScalar[TDocument,TResult](MongoQueryProvider`1 provider, Expression expression)
at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at Sample.Program7.Main2(String[] args) in Program7.cs:line 49
at Sample.Program.Main(String[] args) in Program.cs:line 29
This is the generated pipeline in the V2 version:
[{ "$group": { "_id": 0, "__agg0": { "$sum": { "$cond": [{ "$eq": [ "$CarType", 1 ] }, 1, 0 ] } }, "__agg1": { "$sum": { "$cond": [{ "$eq": [ "$CarType", 2 ] }, 1, 0 ] } } } }, { "$project": { "SportCarCount": "$__agg0", "SuvCarCount": "$__agg1", "_id": 0 } }, { "$limit": 1 } ]