question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect translation of date substraction.

See original GitHub issue
var delays = await db
    .Set<Job>()
    .Select(j => (j.StartJob - j.CreateDate).TotalHours)
    .ToListAsync();

Gets translated into:

SELECT [j].[StartJob] - [j].[CreateDate]
FROM [Job] AS [j]
WHERE [j].[IsDeleted] = 0

Which in turn throws:

System.Data.SqlClient.SqlException (0x80131904): Operand data type datetime2 is invalid for subtract operator.
   at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<BufferlessMoveNext>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
ClientConnectionId:c7a3cc04-ddbf-4aab-9957-7477083c909f
Error Number:8117,State:1,Class:16

This query should either be translated into DateDiff if possible or perform subtraction on client but definitely not throw.

Further technical details

EF Core version: 2.0.1 Database Provider: Microsoft.EntityFrameworkCore.SQLServer Operating system: Windows 10 1703 IDE: Visual Studio 2017

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
divegacommented, Feb 18, 2018

@ralmsdeveloper agreed that there are things that would be missing with that approach. That is what I meant when I said “not all of them will work”. That may be a reason the DateDiff extensions methods are still compelling even if only in some databases.

0reactions
ErikEJcommented, Mar 2, 2018

sql-ce fyi?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the wrong result subtracting a Date&Time from ...
The problem, of course, is that neither years nor months have a fixed number of days, so the differences need to be calculated...
Read more >
My Sql Query. Incorrect value when subtracting two date ...
So I'm subtracting by the current date the value of a date from my database to see the difference between them in days....
Read more >
Why getting a conversion error when subtracting two dates?
I think the problem is that the subtraction of two dates is seen as a date value and when the resulting value is...
Read more >
Solved: trouble with date subtraction
When I use the date field in the table I'm working with it works fine as long as I don't subtract, adding the...
Read more >
Subtracting two dates - algorithms
Dates form an affine space. This means that the result of subtracting two dates is not another date, but rather a time interval....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found