-
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
Using + or - operators between DateTimeOffset.Now and a TimeSpan in a query fail #27028
Comments
@oleneveu What SQL should this translate to? |
@ajcvickers My original message was not clear enough, sorry about that. To be more specific, I will compare two of the samples that I provided in my original message: Flag = field.Date + timeSpan > DateTimeOffset.Now
Flag = field.Date > DateTimeOffset.Now - timeSpan I think that there are two issues:
var successQuery =
from t in context.TestEntities
select new
{
t.Id,
Flag = t.Date + timeSpan > DateTimeOffset.Now
}; translates to: SELECT [t].[Id], [t].[Date], SYSDATETIMEOFFSET()
FROM [TestEntities] AS [t] I suppose that the date calculation is made on the C# side. In any case, the result is correct. But var failureQuery =
from t in context.TestEntities
select new
{
t.Id,
Flag = t.Date > DateTimeOffset.Now - timeSpan
}; translates to: SELECT [t].[Id], CASE
WHEN [t].[Date] > (SYSDATETIMEOFFSET() - @__timeSpan_0) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END AS [Flag]
FROM [TestEntities] AS [t] and fails. Just a guess: As an additional information, the third case from my original message: Flag = field.Date > DateTimeOffset.Now.Subtract(timeSpan) behaves like Flag = field.Date + timeSpan > DateTimeOffset.Now and translates to the exact same SQL: SELECT [t].[Id], [t].[Date], SYSDATETIMEOFFSET()
FROM [TestEntities] AS [t] To go further, when the comparison is made from within the 'where' clause, the execution always ends up with an exception. But once again the behavior is not consistent as the exception is not always the same. Anyway, I understand that my approach was not appropriate as there is not equivalent for TimeSpan on SQL Server. |
Root cause here, there is no type mapping assigned to both side so it escapes our check for throwing exception. |
Using + or - operators between DateTimeOffset.Now and a TimeSpan in a query fail
These 3 lines of code should be equivalent:
Flag = (field.Date + timeSpan) > DateTimeOffset.Now
Flag = field.Date > DateTimeOffset.Now.Subtract(timeSpan)
Flag = field.Date > DateTimeOffset.Now - timeSpan
However the last one fails when the query is executed
Here is a full query that triggers the issue:
This SQL is generated:
When using the 'Substract' method, it seems that the computation is done on the C# side.
Here is a complete project that reproduces the issue :
EFCoreBug.zip
Stack trace
Provider and version information
EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.0.2
The text was updated successfully, but these errors were encountered: