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
Casting from string to int using Cast<int?>() throws exception #28282
Comments
This query fails in regular LINQ to Objects, since you can't use the Cast() operator to convert from the string to int. In general, in EF we try to support patterns which work in regular LINQ. A better way to write this is simply to use Convert to perform your conversion: int currentMaximumLicense = await context.Licenses
.Where(x => x.License.StartsWith("DEMO_"))
.MaxAsync(x => Convert.Int32(x.License.Substring(5)) ?? 0; (or you can use the double-cast pattern as above) |
ahhh, i tried using EDIT: Although i'll have to use int currentMaximumLicense = await context.Licenses
.Where(x => x.License.StartsWith("DEMO_"))
.MaxAsync(x => (int?)Convert.Int32(x.License.Substring(5)) ?? 0; as the sequence might be empty |
Filed #28287 to track translating int.Parse and related, but as discussed we're not going to translate |
Using
Cast<int?>
to cast a string in SQL to int produces an exception. This used to work in EF6.Workaround
Exception
EF Core version: 6.0.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.3 Preview 2
The text was updated successfully, but these errors were encountered: