Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2355

OfType doesn't consider known derived classes

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.7.0
    • Component/s: Linq

      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>());
      

       

       

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            alexb5dh Oleksii Bespalov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: