-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: Sum fails with "specified cast is not valid" when single/double mismatch #7136
Comments
Try casting explicitly to nullable type: var schools = db.Students.Select(p => new { |
I use casting or |
Does it happen if all the students have non-empty Scores collection? db.Students.Where(s=>s.Scores.Any()).Select(p => new { p.Id, p.Name, Score = p.Scores.Sum(s=>s.Score)}); |
Not solved. I convert result to list but have exception yet:
|
This works in current bits. We return 0 for students without scores, and produce the following sql: SELECT [p].[Id], [p].[Name], (
SELECT SUM([s].[Score])
FROM [Scores] AS [s]
WHERE [p].[Id] = [s].[StudentId]
)
FROM [Students] AS [p] |
I testing with latest dev build and not solved.
|
@mojtabakaviani what is the data type of the "Score" column? |
sql: real |
@mojtabakaviani - How did you test with latest dev? what is the version number of EFCore packages? |
@smitpatel I was able to reproduce this on our current bits, working on the fix now... |
…t is not valid Problem was that LINQ expects Sum(Single) to return Single, however SqlServer returns Double. Same goes for Average. Reader returns object of type not expecting (Double), which we try casting to Single, which then leads to cast exceptions. Fix is to explicitly cast result of Sum/Average operations to Single, to better mimic the LINQ behavior for those methods. Customers can up-cast the arguments to Double to avoid potential precision loss.
…t is not valid Problem was that LINQ expects Sum(Single) to return Single, however SqlServer returns Double. Same goes for Average. Reader returns object of type not expecting (Double), which we try casting to Single, which then leads to cast exceptions. Fix is to explicitly cast result of Sum/Average operations to Single, to better mimic the LINQ behavior for those methods. Customers can up-cast the arguments to Double to avoid potential precision loss.
Fixed in f41fcc9 |
Thank you very much. you solving very important bug that must solved very early (ef core 1). |
generated script work perfectly but raise exception when casting:
The text was updated successfully, but these errors were encountered: