Skip to content
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

Closed
mojtabakaviani opened this issue Nov 27, 2016 · 12 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mojtabakaviani
Copy link

var schools = db.Students.Select(p => new { 
      p.Id, 
      p.Name, 
      Score = p.Scores.Sum(s =>  s.Score) 
});

generated script work perfectly but raise exception when casting:

fail: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[1]
      An exception occurred in the database while iterating the results of a query.
      System.InvalidCastException: Specified cast is not valid.
         at lambda_method(Closure , TransparentIdentifier`2 )
         at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
         at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
System.InvalidCastException: Specified cast is not valid.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
@maumar
Copy link
Contributor

maumar commented Nov 27, 2016

Try casting explicitly to nullable type:

var schools = db.Students.Select(p => new {
p.Id,
p.Name,
Score = p.Scores.Sum(s => (int?)s.Score)
});

@mojtabakaviani
Copy link
Author

I use casting or db.Scores.Sum(s => s.Score) but not work. I test Count, Average, Min and Max and work perfectly. just Sum have exception.

@maumar
Copy link
Contributor

maumar commented Nov 28, 2016

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)});

@mojtabakaviani
Copy link
Author

Not solved. I convert result to list but have exception yet:

System.InvalidCastException: Specified cast is not valid.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

@rowanmiller rowanmiller added this to the 1.2.0 milestone Nov 29, 2016
@maumar
Copy link
Contributor

maumar commented Apr 13, 2017

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]

@maumar maumar closed this as completed Apr 13, 2017
@maumar maumar added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed type-investigation labels Apr 13, 2017
@mojtabakaviani
Copy link
Author

mojtabakaviani commented Apr 18, 2017

I testing with latest dev build and not solved.

fail: Microsoft.EntityFrameworkCore.Query[1]
      An exception occurred in the database while iterating the results of a query.
      System.InvalidCastException: Specified cast is not valid.
         at lambda_method(Closure , QueryContext , TransparentIdentifier`2 )
         at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper.TypedProjectionShaper`3.Shape(QueryContext queryContext, ValueBuffer valueBuffer)
         at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
         at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
System.InvalidCastException: Specified cast is not valid.
   at lambda_method(Closure , QueryContext , TransparentIdentifier`2 )
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper.TypedProjectionShaper`3.Shape(QueryContext queryContext, ValueBuffer valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

@maumar
Copy link
Contributor

maumar commented Apr 18, 2017

@mojtabakaviani what is the data type of the "Score" column?

@maumar maumar reopened this Apr 18, 2017
@maumar maumar added type-investigation and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Apr 18, 2017
@mojtabakaviani
Copy link
Author

sql: real
dotnet: float

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@smitpatel
Copy link
Contributor

@mojtabakaviani - How did you test with latest dev? what is the version number of EFCore packages?

@maumar
Copy link
Contributor

maumar commented Apr 20, 2017

@smitpatel I was able to reproduce this on our current bits, working on the fix now...

maumar added a commit that referenced this issue Apr 20, 2017
…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.
maumar added a commit that referenced this issue Apr 21, 2017
…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.
@maumar
Copy link
Contributor

maumar commented Apr 21, 2017

Fixed in f41fcc9

@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 21, 2017
@mojtabakaviani
Copy link
Author

Thank you very much. you solving very important bug that must solved very early (ef core 1).

@ajcvickers ajcvickers modified the milestones: 2.0.0, 2.0.0-preview1 Apr 24, 2017
@ajcvickers ajcvickers changed the title ef 1.1 sum of collection raise exception Specified cast is not valid Query: Sum fails with "specified cast is not valid" when single/double mismatch May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

6 participants