InvalidCastException with LinqProvider V3 when passing string as DateTime

Hello,

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?

Hi, @Laurynas,

Welcome to the MongoDB Community Forums. I have reproduced the issue that you encountered when switching from the LINQ2 to LINQ3 provider. I created CSHARP-4609 to track this bug. Thank you for reporting this issue. Please follow CSHARP-4609 for updates.

Sincerely,
James

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.