For the following inheritance chain:
[BsonKnownTypes(typeof(B))] public class A { public ObjectId Id { get; set; } } [BsonKnownTypes(typeof(C))] public class B: A { } public class C: B { }
querying with OfType wih the base type B doesn't consider derived classes added via BsonKnownTypes:
var collection = new MongoClient("mongodb://localhost/") .GetDatabase("Test") .GetCollection<A>(Guid.NewGuid().ToString()); collection.InsertOne(new C()); Assert.Single(collection.AsQueryable().OfType<B>().AsEnumerable());
while for regular IEnumerable it does:
var collection = new List<A> { new C() }; Assert.Single(collection.OfType<B>());