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

OfType doesn't consider known derived classes

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Works as Designed
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.7.0
    • Linq

    Description

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

       

       

       

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: