Hi everyone,

It seems that casting via the IVehicle interface is not supported in LinqProvider.V3.
When attempting to filter based on a field inside a discriminated subtype, the following query fails:

var user = await collection.Find(x => ((Car)x.Vehicle!).Model == "Test").FirstOrDefaultAsync();

This throws the following exception:

MongoDB.Driver.Linq.ExpressionNotSupportedException : Expression not supported: Convert(x.Vehicle, Car) because conversion to MongoCases.MongoNestedDiscriminatedUnionCases+Car is not supported.

However, this exact code worked fine in LinqProvider.V2. After upgrading to LinqProvider.V3, the query no longer functions as expected.

Additionally, if I replace IVehicle (an interface) with a regular class, everything works fine. Unfortunately, changing IVehicle to a class is not an option for my case.

Is there a way to make this work while keeping IVehicle as an interface?

Any suggestions or workarounds would be greatly appreciated!


Full Reproduction Code:

using System; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver; using MongoDB.Driver.Linq; using NUnit.Framework; namespace MongoCases { [TestFixture] public class MongoNestedDiscriminatedUnionCases : Mongo2GoFixture { [Test] public async Task Test() { BsonSerializer.RegisterSerializer(new ObjectSerializer(_ => true)); var mongoClientSettings = MongoClientSettings.FromConnectionString(ConnectionString); mongoClientSettings.LinqProvider = LinqProvider.V3; var mongoClient = new MongoClient(mongoClientSettings); var db = mongoClient.GetDatabase(Guid.NewGuid().ToString().Substring(0, 8)); var collection = db.GetCollection<User>("users"); await collection.InsertOneAsync(new User()); await collection.InsertOneAsync(new User { Vehicle = new Car { Model = "Test" } }); var user = await collection.Find(x => ((Car)x.Vehicle!).Model == "Test").FirstOrDefaultAsync(); // MongoDB.Driver.Linq.ExpressionNotSupportedException : Expression not supported: Convert(x.Vehicle, Car) // because conversion to MongoCases.MongoNestedDiscriminatedUnionCases+Car is not supported. Assert.That(user, Is.Not.Null); } private class User { [BsonId] public ObjectId Id { get; set; } [BsonElement("vehicle"), BsonIgnoreIfNull] public IVehicle? Vehicle { get; set; } } private interface IVehicle { } [BsonDiscriminator("car")] private class Car : IVehicle { [BsonElement("model"), BsonRequired] public string Model { get; set; } } [BsonDiscriminator("bike")] private class Bike : IVehicle { } } }
BsonClassMap.RegisterClassMap<Post>(map => { map.AutoMap(); map.SetIsRootClass(true); map.AddKnownType(typeof(Poll)); map.AddKnownType(typeof(Article)); });
17 days later

Hi @flibustier_seas ,

I cannot reproduce the problem using the latest code base. Could you please specify which version of MongoDB Driver are you using?

Thanks

Hi!

<PackageReference Include="MongoDB.Bson" Version="2.25.0" /> <PackageReference Include="MongoDB.Driver" Version="2.25.0" />

Let me know if you need any other details!

Hi!

Thanks for your suggestion, but unfortunately, it doesn’t work with interfaces:

MongoDB.Bson.BsonSerializationException : Discriminators can only be registered for classes, not for interface MongoCases.MongoNestedDiscriminatedUnionCases+IVehicle.