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

Casting to double not supported in LINQ Select expression

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.19.0
    • Affects Version/s: None
    • Component/s: Linq

      I'm using decimals to store my data on mongoDB, because I need to have as much precision as I can.

      To use $sqrt on mongoDB you need to pass a double as argument, so I need to cast my decimal to double and then pass it to $sqrt.
      Using mongoDB shell it's an easy job, but when you try to code this using mongoDB c# driver with IQueryable interface things getting complicated.

      My code:

      var data = _myRepository.AsQueryable()
         .GetValues(equipmentId, startDate, endDate)
         .GroupBy(g => g.timestamp)
         .Select(p => new MyDTO {
            timestamp = p.Key,
            sqrt_calc = (decimal)Math.Sqrt(
               (double)p.Sum(x => x.my_decimal_value)
            )
         .OrderBy(q => q.timestamp);

      I'm getting the following exception: OverflowException: Arithmetic operation resulted in an overflow.

      IQueryable is producing the query into query.txt attached.query.txt

      As you can see there is a missing $toDouble after $sqrt, so $sqrt is receiving a decimal and produces a NaN which I believe that is causing the exception.

      Is there a way to tell IQueryable to add the $toDouble cast?

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            dcraveiro@eneida.io David Craveiro
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: