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

Support LINQ queries after casting IQueryable<Derived> to IQueryable<Base>

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 3.4.0
    • Affects Version/s: None
    • Component/s: LINQ
    • None
    • Fully Compatible
    • Needed
    • Hide

      Probably doesn't need documentation.

      Show
      Probably doesn't need documentation.
    • None
    • None
    • None
    • None
    • None
    • None

      I have a public class `Entity` and an entity that inherits from that called `MongoEntity`.

      The MongoEntity is an implementation detail of the repository, so it is used only internally and is not exposed outside.

      e.g. something like this:

       

      class Entity {}
      class MongoEntity : Entity {}
      public class Repository{
         private IMongoCollection<MongoEntity> collection;
         
         public IQueryable<Entity> Entities {
             get => collection.AsQueryable();
         }
      }
      

      Until 2.18.0 it works fine, but it breaks with 2.19.0:

      I get the following exception in my sample:

      System.ArgumentException: 'ValueType 'ConsoleApp4.Program+MongoEntity' of parameterSerializer does not match parameter type 'ConsoleApp4.Program+Entity'. (Parameter 'parameterSerializer')'

      In my real application I get the following error:

      System.InvalidCastException: 'Unable to cast object of type 'MongoDB.Bson.Serialization.BsonClassMapSerializer`1[Notifo.Identity.MongoDb.MongoDbUser]' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[Microsoft.AspNetCore.Identity.IdentityUser]'.'

      I was not able to reproduce this exception in my sample, but I guess it is the same root issue.

      My very little example is attached.

        1. ConsoleApp4.csproj
          0.3 kB
        2. Program.cs
          1 kB

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            sebastian@squidex.io Sebastian Stehle
            Votes:
            4 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: