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

Handle conversion to an interface

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.18.0
    • Affects Version/s: 2.17.1
    • Component/s: None
    • Labels:
      None
    • Fully Compatible

      the following exception is being thrown by linq v3 if a simple join query is performed within a generic method:

      > System.ArgumentException: 'Invalid toType: IEntity. (Parameter 'toType')'

      here's how to reproduce it:

      the classes

      public interface IEntity
      {
          string Id { get; set; }
      }
      
      public class Parent : IEntity
      {
          [BsonId]
          public string Id { get; set; }
      
          [BsonRepresentation(BsonType.ObjectId)]
          public string ChildId { get; set; }
      }
      
      public class Child : IEntity
      {
          [BsonId]
          public string Id { get; set; }
      }
      

      the program

      var children = GetChildrenQueryable<Child>(db, "Child");
      children.Count();
      
      private IMongoQueryable<TChild> GetChildrenQueryable<TChild>(IMongoDatabase db, string childCollectionName) where TChild : IEntity
      {
          var parentCollection = db.GetCollection<Parent>("Parent");
          var childCollection = db.GetCollection<TChild>(childCollectionName).AsQueryable();
      
          return parentCollection
              .AsQueryable()
              .Join(
                  childCollection,
                  p => p.ChildId,
                  c => c.Id,
                  (_, c) => c);
      }
      

      this works perfectly fine with line v2.

      would highly appreciate a workaround until this can be fixed.

      thanks!

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            djnitehawk83@gmail.com Damith G
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: