Home > Enterprise >  DateDiff using Entity Framework
DateDiff using Entity Framework

Time:10-10

I'm trying to use DateDiff in Entity Framework:

var data = (from e in _context.Employers
            join t in _context.Tickets on e.Id equals t.IdEmployer
            join i in _context.InfoClients on t.IdConnexion equals i.Id
            where t.Etat == "Encours"
            select new
                   {
                       t.DateCreation,
                       t.NumTicket,
                       i.IdConnexion,
                       datediff = DateTime.Now.Subtract((DateTime)t.DateCreation).TotalDays,
                       t.SujetPrblm,
                       t.IdTicket,
                       e.Nom,
                       e.Prenom
                   }).ToList();

But I'm getting this error:

This function can only be invoked from linq to entities

I also tried this but get the same error:

DateDiff = SqlFunction.DateDiff("Hour", t.DateCreation, DateTime.Now)

CodePudding user response:

You can select DateCreationin select and then you can get the DateTime.Now.Subtract whenever you need later in code.

CodePudding user response:

You can try like

 DateTime.Now.Date.Subtract((yourDateTimeHere).Date).Days

and replace yourDateTimeHere.

  • Related