Efcore: DBFunctions.CreateDateTime

Created on 4 Jun 2018  路  9Comments  路  Source: dotnet/efcore

Hi, I'd like to be able to do queries like the following:

C# _context.Shifts .Include(w => w.ShiftDefinition) .Select(w => new { w, dt = DbFunctions.CreateDateTime( w.ShiftDay.Year, w.ShiftDay.Month, w.ShiftDay.Day, w.ShiftDefinition.StartTime.Hours, w.ShiftDefinition.StartTime.Minutes, w.ShiftDefinition.StartTime.Seconds) }) .Where(w => currentDate < w.dt) .OrderBy(w => w.dt) .Select(q => q.w) .FirstOrDefault();
But it seems that DBFunctions.CreateDateTime missing.
Is it planned to get that function back, or is there any equivalent?
Regards. PV

area-query closed-fixed customer-reported good first issue type-enhancement

Most helpful comment

I was porting code from EF6 to EF core, then I supposed that that limitation was going to be the same in EF core. I can't try my the code at the moment, but my quick question would be: Is 'new DateTime' captured as an expression and translated into sql?

All 9 comments

@paulovila Is there a reason you want to use DbFunctions.CreateDateTime instead of new DateTime(...)?

I was porting code from EF6 to EF core, then I supposed that that limitation was going to be the same in EF core. I can't try my the code at the moment, but my quick question would be: Is 'new DateTime' captured as an expression and translated into sql?

@smitpatel Is new DateTime translated yet, at least on SQL Server?

No.

@divega to investigate what we did in EF6

What's the state here? I a also port from EF 6 to EF Core and also want this translated into the query.

We have a .GroupBy() clause and then select the following with included null coalesce operations, and want that to be executed on the DB:

.Select(group => new
    {
        In = group.Sum(entry => entry.ContentBytesIn),
        Out = group.Sum(entry => entry.ContentBytesOut),
        Key = DbFunctions.CreateDateTime(group.Key.Year, group.Key.Month ?? 1, group.Key.Day ?? 1, 0, 0, 0).Value,
    })
    .OrderBy(entry => entry.Key)

@gingters per previous comment, we're waiting for @divega to investigate.

Almost forgot about this, needless to say it was like 10 months ago ;) . Is there any plan at all to make it work with "new Datetime(" or "CreatrDatetime" ?.

Hi @divega and @ajcvickers
I've opened a PR with an initial attempt to implement some similiar functionality as we used to have at EF6 with DBFunctions.CreateDate, please let me know if I'm going the right way. Initially this is targeting SqlServer only.

Was this page helpful?
0 / 5 - 0 ratings