-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: 3.4.0
-
Component/s: LINQ3
-
None
-
None
-
Dotnet Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Summary
3.4.0 introduced a bug that prevents the Key field of a KVP from being selected when using camelCase.
System.FormatException: 'Element 'Key' does not match any field or property of class System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].'
How to Reproduce
using MongoDB.Bson.Serialization.Conventions; using MongoDB.Driver; using MongoDB.Driver.Linq; var defaultPack = new ConventionPack { new CamelCaseElementNameConvention() }; ConventionRegistry.Register("camelCase", defaultPack, c => true); var client = new MongoClient("mongodb://localhost:27017"); var collection = client.GetDatabase("kvp_bug").GetCollection<Person>(nameof(Person)); await collection.InsertOneAsync(new Person() { Title = "Other" }); await collection .AsQueryable() .GroupBy(x => x.Title) .Select(b => new KeyValuePair<string, int>(b.Key, b.Count())) .ToListAsync(); public class Person { public string Title { get; set; } }
I believe it's caused by NewKeyValuePairExpressionToAggregationExpressionTranslator.cs hardcoding the Key member which was introduced by https://github.com/mongodb/mongo-csharp-driver/pull/1661.