when adding filter like this
Builders<MeasurementDetails>.Filter.Lt(field, myValue);
where field is Expression<Func<MyObjectType, object>> field = x => x.CreationDate
and CreationDate property type is DateTime, but myValue is string (because we filter dynamically depending on user request)
LinqProvider V2 works fine, but V3 version throws exception
System.InvalidCastException : Unable to cast object of type 'System.String' to type 'System.DateTime'. at MongoDB.Bson.Serialization.Serializers.DowncastingSerializer`2.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBase value) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize[TValue](IBsonSerializer`1 serializer, BsonSerializationContext context, TValue value) at MongoDB.Driver.OperatorFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.OrFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider) at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options) at MongoDB.Driver.MongoCollectionImpl`1.FindAsync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass48_0`1.<FindAsync>b__0(IClientSessionHandle session) at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
So it seems on this line in DowncastingSerializer it throws exception because it simply tries to cast string to DateTime. Not sure if that is intended behavior in new Linq version or a bug?
– created from https://www.mongodb.com/community/forums/t/invalidcastexception-with-linqprovider-v3-when-passing-string-as-datetime/221558/1
- is caused by
-
CSHARP-4499 Support Convert calls to a base type in filter translators
- Closed