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

LINQ3 provider: ValueType 'X' of parameterSerializer does not match parameter type 'Y' (where X is a subclass of Y)

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.19.1
    • Component/s: LINQ3
    • Labels:
      None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      The latest MongoDB.Driver for C# switched the default LINQ Provider from 2 to 3. After trying to upgrade and use version 3 the code that used to work in V2 fails in V3.

      Server versions: 5.0.13 Windows and 5.0.10 Ubuntu

      How to Reproduce

          internal class Program
          {
              private static void Main(string[] args)
              {
                  foreach (var linqProvider in new []\{ LinqProvider.V2, LinqProvider.V3})
                  {
                      var builder = new MongoUrlBuilder
                      {
                          DatabaseName = "Sample",
                          Server = new MongoServerAddress("localhost", 27017)
                      };
                      var mongoClientSettings = MongoClientSettings.FromUrl(builder.ToMongoUrl());
                      mongoClientSettings.LinqProvider = linqProvider;
                      var mongoClient = new MongoClient(mongoClientSettings);
                      var mongoDatabase = mongoClient.GetDatabase(builder.DatabaseName);
      
                      var licensePlate = "5555XXX";
      
                      var carCollection = mongoDatabase.GetCollection<Car>("Cars");
                      carCollection.DeleteMany(c => true);
                      carCollection.InsertOne(new Car
                      {
                          Id = licensePlate,
                          Description = $"\{linqProvider} - Description for license: \{licensePlate}"
                      });
      
                      var deletedCarCollection = mongoDatabase.GetCollection<DeletedCar>("DeletedCars");
                      deletedCarCollection.DeleteMany(c => true);
                      deletedCarCollection.InsertOne(new DeletedCar
                      {
                          Id = licensePlate,
                          Description = $"\{linqProvider} - Deleted - Description for license: \{licensePlate}",
                          DeletedDate = DateTime.UtcNow.AddDays(-1)
                      });
      
                      Console.WriteLine(GetCarDescription_Works(carCollection.AsQueryable(), licensePlate));
                      Console.WriteLine(GetCarDescription_Works(deletedCarCollection.AsQueryable(), licensePlate));
                      Console.WriteLine();
                      Console.WriteLine(GetCarDescription_Fails(carCollection.AsQueryable(), licensePlate));
                      Console.WriteLine(GetCarDescription_Fails(deletedCarCollection.AsQueryable(), licensePlate));
                      Console.WriteLine();
                      Console.WriteLine();
                  }
              }
      
              private static string GetCarDescription_Works<T>(IQueryable<T> queryable, string licensePlate) where T : Car
              {
                  return queryable.Where(d => d.Id == licensePlate).Select(d => d.Description).FirstOrDefault();
              }
      
              private static string GetCarDescription_Fails(IQueryable<Car> queryable, string licensePlate)
              {
                  return queryable.Where(d => d.Id == licensePlate).Select(d => d.Description).FirstOrDefault();
              }
      
              private class Car
              {
                  public string Id \{ get; set; }
                  public string Description \{ get; set; }
              }
      
              private class DeletedCar : Car
              {
                  public DateTime DeletedDate \{ get; set; }
              }
          }
      

       

      Execution output:

      Description for license: 5555XXX
      Deleted - Description for license: 5555XXX
      
      Description for license: 5555XXX
      
      Unhandled Exception: System.ArgumentException: ValueType 'Sample.Program+DeletedCar' of parameterSerializer does not match parameter type 'Sample.Program+Car'.
      Parameter name: parameterSerializer
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateLambda(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.WhereMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.SelectMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.FirstMethodToExecutableQueryTranslator`1.Translate[TDocument](MongoQueryProvider`1 provider, TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.TranslateScalar[TDocument,TResult](MongoQueryProvider`1 provider, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](Expression expression)
         at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
         at Sample.Program.GetCarDescription_Fails(IQueryable`1 queryable, String licensePlate) in D:\tfs2015\Shared\Packages\Panda.DataAccess\Sample\Program.cs:line 53
         at Sample.Program.Main(String[] args) in D:\tfs2015\Shared\Packages\Panda.DataAccess\Sample\Program.cs:line 43
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            suikevil Roberto Pérez
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: