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
;
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)
```
- duplicates
-
CSHARP-4499 Support Convert calls to a base type in filter translators
- Closed