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

Support Convert calls to a base type in filter translators

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.19.1
    • Affects Version/s: 2.19.0
    • Component/s: LINQ3
    • 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

      In our solution we are generating dynamic queries based on generics. The folowing snippet worked with Linq2 provider but since the new update (MongoDB.Driver 2.19.0) its broken in the Linq3 provider. __ 

      It has something to do with object type of the property. String property works while ObjectId property doesn't. Looking at how .NET renders the expression it adds a Convert() method around the ObjectId property while not doing that for a string property. 

      How to Reproduce

      No MongoDb instance needed as this just fails to render the query. Attached basic console app reproducing the scenario.

      ConsoleApp4.zip

      Failing part:

      Expression<Func<ConcreteClass, object>> expression = x => x.InternalId;
      
       var fieldDefinition = new ExpressionFieldDefinition<ConcreteClass, object>(expression);
      
      var query = collection
              .Aggregate()
              .Sort(Builders<ConcreteClass>.Sort.Ascending(fieldDefinition))
              .ToString();

       

      Additional Background

      StackTrace:

      Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: Convert(x.InternalId, Object).
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ConvertExpressionToFilterFieldTranslator.Translate(TranslationContext context, UnaryExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ExpressionToFilterFieldTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToField[TDocument,TField](Expression`1 expression, IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, Boolean allowScalarValueForArrayField)
         at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider, Boolean allowScalarValueForArrayField)
         at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
         at MongoDB.Driver.UntypedFieldDefinitionAdapter`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
         at MongoDB.Driver.DirectionalSortDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
         at MongoDB.Driver.SortPipelineStageDefinition`1.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
         at MongoDB.Driver.AppendedStagePipelineDefinition`3.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
         at MongoDB.Driver.PipelineDefinition`2.ToString(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)
         at MongoDB.Driver.PipelineDefinition`2.ToString(LinqProvider linqProvider)
         at MongoDB.Driver.AggregateFluent`2.ToString()

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            danik.raikhlin@outlook.com DanikRaikhlin N/A
            Votes:
            3 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: