You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think SqlServerTimeSpanMemberTranslator can be useful also to other .NET developers, so if you want I can make a PR to include it to EFCore.SqlServer project.
@aleksandar-manukov Yes, we would be interested in a PR for this. We're curious how it deals with time spans that cover more than 24 hours, where the naive mapping to T-SQL time doesn't work.
Never needed to deal with TimeSpan which is more than 24 hours. Our use case is that we have a time column for execution schedules and we need to compare it to current hours and minutes and to compare its time parts to other datetime column time parts which is holding execution schedules history. So we use the following query
// This is selecting all schedulers with scheduled time equal or bigger to the current time (hours and minutes) and for which there is no record in the StepScheduleHistory for today with time bigger than the scheduled.
return await _dbContext.StepSchedules
.Where(ss => ss.StepId == stepId &&
(ss.ScheduledTime.Hours > DateTime.Now.Hour ||
(ss.ScheduledTime.Hours == DateTime.Now.Hour &&
ss.ScheduledTime.Minutes >= DateTime.Now.Minute)) &&
!_dbContext.StepScheduleHistories
.Any(ssh => ssh.StepScheduleId == ss.Id &&
ssh.ExecutionDate.Day == DateTime.Today.Day &&
ssh.ExecutionDate.Month == DateTime.Today.Month &&
ssh.ExecutionDate.Year == DateTime.Today.Year &&
(ssh.ExecutionDate.Hour > ss.ScheduledTime.Hours ||
(ssh.ExecutionDate.Hour == ss.ScheduledTime.Hours &&
(ssh.ExecutionDate.Minute > ss.ScheduledTime.Minutes ||
(ssh.ExecutionDate.Minute == ss.ScheduledTime.Minutes &&
ssh.ExecutionDate.Second >= ss.ScheduledTime.Seconds))))))
.MapTo<StepScheduleViewModel>()
.OrderBy(ss => ss.ScheduledTime)
.ToListAsync();
This query works with EntityFramework 6 but in EntityFramework Coress.ScheduledTime.Hours, ss.ScheduledTime.Minutes and ss.ScheduledTime.Seconds cannot be translated to SQL. That's why we make SqlServerTimeSpanMemberTranslator like the SqlServerDateTimeMemberTranslator which is translating for example ss.ScheduledTime.Hours to datepart(hour, [ScheduledTime]).
Do you want me to make a PR and to continue discussion there or you need something more advanced to handle TimeSpan members translation?
I have never meant to translate TimeSpan members with datediff and dateadd functions. What we use (as you can see in my previous comment) is to translate TimeSpan.Hours -> datepart(hour, @TimeSpan) which is currently missing in EF Core. If you want I can make a PR to add SqlServerTimeSpanMemberTranslator to translate TimeSpan members to appropriate datepart function.
@bricelam, initially tried to do a datediff (this works with time sql value), but I don't think this is possible to do with better precision than milliseconds. I ended up using datepart to get each component separately, then multiplying those values by the correct ratio to get the total nanoseconds. Then I would divide that value by the appropriate ratio for the time units requested. This ended up being quite a big piece of SQL, but I don't think it would be that slow since it is essentially just extracting a few values from the time data and then multiplying and dividing a constant amount of times.
@SoftCircuits You have to write it like this for now: g.Average(t => EF.Functions.DateDiffMinute(t.Departure!.Value, t.Arrival))
These translations for Total* properties could already be created, and it would work for TimeSpans bigger than 24h because the difference between dates returns a TimeSpan but accessing any member of the returned TimeSpan returns an integer.
But it would be a partial translation, only suitable for diffences of DateTimes and DateTimeOffsets.
Also the two arguments for EF.Functions.DateDiff* functions need to be swapped because the sign returned is inverted.
I'd like to take the Total* members (TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds) on SQL Server, computed from DATEPART over the time value so it works on all supported versions, with tests in the TimeSpan translation suites. I'll open a PR so it can be reviewed.
31. I'd like to take the Total* members (TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds) on SQL Server, computed from DATEPART over the time value so it works on all supported versions, with tests in the TimeSpan translation suites. I'll open a PR so it can be reviewed.
I'm not sure but I think that Total* members might not be suitable for DATEPART, even with time. Because for time 12:30 hours part is 12 while TotalHours would be 12.5
Right, a single DATEPART isn't enough. The idea is to add up all the parts into the exact number of ticks (hour * 3600 + minute * 60 + second, plus the fraction from DATEPART(nanosecond)) and divide once, as .NET does, so 12:30 gives 12.5 for TotalHours.
Activity
ajcvickers commentedon Jan 21, 2020
@aleksandar-manukov Yes, we would be interested in a PR for this. We're curious how it deals with time spans that cover more than 24 hours, where the naive mapping to T-SQL
timedoesn't work.aleksandar-manukov commentedon Jan 22, 2020
Hi @ajcvickers
Never needed to deal with
TimeSpanwhich is more than 24 hours. Our use case is that we have atimecolumn for execution schedules and we need to compare it to current hours and minutes and to compare its time parts to otherdatetimecolumn time parts which is holding execution schedules history. So we use the following queryThis query works with
EntityFramework 6but inEntityFramework Coress.ScheduledTime.Hours,ss.ScheduledTime.Minutesandss.ScheduledTime.Secondscannot be translated to SQL. That's why we make SqlServerTimeSpanMemberTranslator like the SqlServerDateTimeMemberTranslator which is translating for exampless.ScheduledTime.Hourstodatepart(hour, [ScheduledTime]).Do you want me to make a PR and to continue discussion there or you need something more advanced to handle
TimeSpanmembers translation?ajcvickers commentedon Jan 22, 2020
@aleksandar-manukov Thanks for the additional info. We will discuss.
bricelam commentedon Jan 24, 2020
DATEPART(hour, @TimeSpan)DATEPART(minute, @TimeSpan)DATEPART(second, @TimeSpan)DATEPART(millisecond, @TimeSpan)bricelam commentedon Jan 24, 2020
The DATEDIFF and DATEADD functions don't really have .NET equivalents.
aleksandar-manukov commentedon Jan 24, 2020
Hi @bricelam
I have never meant to translate
TimeSpanmembers withdatediffanddateaddfunctions. What we use (as you can see in my previous comment) is to translateTimeSpan.Hours -> datepart(hour, @TimeSpan)which is currently missing in EF Core. If you want I can make a PR to add SqlServerTimeSpanMemberTranslator to translateTimeSpanmembers to appropriatedatepartfunction.bricelam commentedon Jan 24, 2020
A PR would be awesome!
Oh I was just doing my due diligence to see what other members we could translate. I only found
Milliseconds.bricelam commentedon Jan 24, 2020
(You may have already seen it, but I have work-in-progress PR out for TimeSpan translations on SQLite)
bricelam commentedon Jan 24, 2020
@cowmanjoe What SQL were you proposing for the Total* members in PR #19643?
aleksandar-manukov commentedon Jan 25, 2020
@bricelam thanks. I will check your PR especially to see what tests I need to do and I will try to make a PR soon.
cowmanjoe commentedon Jan 27, 2020
@bricelam, initially tried to do a
datediff(this works withtimesql value), but I don't think this is possible to do with better precision than milliseconds. I ended up usingdatepartto get each component separately, then multiplying those values by the correct ratio to get the total nanoseconds. Then I would divide that value by the appropriate ratio for the time units requested. This ended up being quite a big piece of SQL, but I don't think it would be that slow since it is essentially just extracting a few values from thetimedata and then multiplying and dividing a constant amount of times.SqlServer: Translate TimeSpan members
7 remaining items
bricelam commentedon Jan 12, 2023
@SoftCircuits You have to write it like this for now:
g.Average(t => EF.Functions.DateDiffMinute(t.Departure!.Value, t.Arrival))roji commentedon Oct 9, 2025
Note: also translate the Total* members.
Xriuk commentedon Oct 9, 2025
These translations for Total* properties could already be created, and it would work for TimeSpans bigger than 24h because the difference between dates returns a TimeSpan but accessing any member of the returned TimeSpan returns an integer.
But it would be a partial translation, only suitable for diffences of DateTimes and DateTimeOffsets.
Also the two arguments for EF.Functions.DateDiff* functions need to be swapped because the sign returned is inverted.
Laurianti commentedon Oct 31, 2026
I'd like to take the
Total*members (TotalDays,TotalHours,TotalMinutes,TotalSeconds,TotalMilliseconds) on SQL Server, computed fromDATEPARTover thetimevalue so it works on all supported versions, with tests in the TimeSpan translation suites. I'll open a PR so it can be reviewed.Xriuk commentedon Oct 31, 2026
I'm not sure but I think that Total* members might not be suitable for DATEPART, even with time. Because for time 12:30 hours part is 12 while TotalHours would be 12.5
Laurianti commentedon Oct 31, 2026
Right, a single
DATEPARTisn't enough. The idea is to add up all the parts into the exact number of ticks (hour * 3600 + minute * 60 + second, plus the fraction fromDATEPART(nanosecond)) and divide once, as .NET does, so 12:30 gives 12.5 forTotalHours.Translate the TimeSpan Total* members on SQL Server (dotnet#39487)