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

LINQ3 does not allow cast to float

    • Type: Icon: Question Question
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.24.0
    • Component/s: LINQ3
    • Labels:
      None
    • Dotnet Drivers
    • 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

      Issues with LinqProvider.V3

      Driver Version: C# driver 2.24.0

      How to Reproduce

      var connectionString = "mongodb://localhost:27017";
      var setting = MongoClientSettings.FromConnectionString(connectionString);
      setting.LinqProvider = LinqProvider.V3;
      var mongoClient = new MongoClient(setting);
      
      var db = mongoClient.GetDatabase("test");
      var coll = db.GetCollection<CallRecord>("calls");
      
      var result = coll.AsQueryable()
          .Select(p => new {name= p.PhoneNumber,DurationInMinute = Math.Ceiling((float)p.Duration / 60) });
      
      var array = await result.ToListAsync();
      
      foreach (var record in array)
      {
          Console.WriteLine($"phone:{record.name},Duration:{record.DurationInMinute}");
      }
      
      public record CallRecord(string PhoneNumber,int Duration); 

      As shown in the code, if setting.LinqProvider = LinqProvider.V2 is set, it can run normally. However, if setting.LinqProvider = LinqProvider.V3 is set, the following error occurs:
       

      Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: Convert(p.Duration, Single) because conversion to System.Single is not supported.
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ConvertExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, UnaryExpression expression)

       

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            gw2008@gmail.com p3 p3
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: