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

Casting from an interface to a concrete type in a filter expression fails

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.19.0
    • Component/s: LINQ3

      The following code fails in Linq V3 but works if I cahnge to LINQ v2:

      var connectionString = "mongodb://localhost/";
      var settings = MongoClientSettings.FromConnectionString(connectionString);
      settings.LinqProvider = MongoDB.Driver.Linq.LinqProvider.V3;    
      var client = new MongoClient(settings);
      var database = client.GetDatabase("testdb");
      var collection = database.GetCollection<IMyInterface>("xx_test_collection");
      var filter = Builders<IMyInterface>.Filter.Eq(x=>((MyClass)x).TestValue, 42);
      var findFluent = collection.Find(filter);
      Console.WriteLine(findFluent.ToString()); 

      Classes and interfaces are very basic and as follows:

      public interface IMyInterface { }
      public class MyClass : IMyInterface
      {
          public int TestValue { get; set;}
      }
       
      
      
      

      When LinqProvider is set to V2 it correctly outputs 

      find({ "TestValue" : 42 })

      If I run the code with the V3 provider I get:

      Expression not supported: Convert(x)

       

      My assumption is that the issue lies in the code in ConvertExpressionToFilterFieldTranslator where IsConvertToDerivedType feels like it should return true but because it checks for IsSubclassOf rather than IsAssignableFrom it decides that it can't convert.

      Also I am aware that I can rewrite this using OfType but I figure if it works in V2 then it should work in V3 too.

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            chris@passle.net Chris Venus
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: