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

Support Convert calls to a base type in expression translators

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.19.1
    • Affects Version/s: None
    • Component/s: None
    • Not Needed
    • 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

      When migrating to LINQ provider v3, sorting no longer works in cases where the key selector expression has a compile time type of `System.Object` (but the appropriate runtime type).

      We discovered this when migrating to driver version 2.19.0, where LINQ3 is the new default. If we manually set the LINQ provider to v2, things go back to working as expected.

      How to Reproduce

      See attached for a minimal console app that reproduces the behavior. Notable lines:

      ```
      Expression<Func<DbJob, object>> selector = sortOrder switch

      {     JobSortColumn.JobNumber => j => j.JobNumber,     JobSortColumn.DriverName => j => j.DriverName, }

      ;

      jobsQueryable = jobsQueryable.OrderByDescending(selector);
      var jobs = await jobsQueryable.ToListAsync(); // error if LINQ provider is v3
      ```

      The resulting exception if LINQ3 is enabled:

      ```

      Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: Convert(j.JobNumber, Object) because conversion to System.Object is not supported.
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ConvertExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, UnaryExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.UnaryExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, UnaryExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.OrderByMethodToPipelineTranslator.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.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.ToCursorAsync(CancellationToken cancellationToken)
         at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
         at Program.<Main>$(String[] args) in /Users/mirek/RiderProjects/ConsoleApp1/ConsoleApp1/Program.cs:line 21
         at Program.<Main>(String[] args)

      ```

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            mirek@rynly.com Mirek Kukla
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: