[CSHARP-2472] Casting to double not supported in LINQ Select expression Created: 08/Jan/19 Updated: 08/Feb/23 Resolved: 03/Nov/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq |
| Affects Version/s: | None |
| Fix Version/s: | 2.19.0 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | David Craveiro | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 0 |
| Labels: | rfw | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
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. My code: var data = _myRepository.AsQueryable() 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? |
| Comments |
| Comment by Githook User [ 07/Nov/22 ] | ||||||||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | ||||||||||||||||||||||||||
| Comment by Githook User [ 03/Nov/22 ] | ||||||||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | ||||||||||||||||||||||||||
| Comment by Robert Stam [ 03/Nov/22 ] | ||||||||||||||||||||||||||
|
Thanks for the excellent repro, that made it much easier to work on this! I got this working, but I had to make a very small change to your repro code:
The change is that:
becomes:
The cast to `double` had been moved to inside the call to `Sum`. This change is necessary because `decimal` is represented by default as a string, and we need to convert those strings to doubles before we can sum them. The generated pipeline is:
| ||||||||||||||||||||||||||
| Comment by David Craveiro [ 12/Feb/19 ] | ||||||||||||||||||||||||||
|
GetValues is an Expression to match documents. After replacing it this is the code I have:
| ||||||||||||||||||||||||||
| Comment by Robert Stam [ 11/Feb/19 ] | ||||||||||||||||||||||||||
|
I've changed the Summary of the ticket to reflect that it is the cast to double that is not supported. To help in reproducing this in the future, can you please explain what this line is:
it doesn't look like standard LINQ.
| ||||||||||||||||||||||||||
| Comment by Wan Bachtiar [ 11/Feb/19 ] | ||||||||||||||||||||||||||
Hi, Unfortunately $toDouble aggregation operator as LINQ is not currently supported.
Regards, |