Calculate date difference and compare with integer field in lambda expression

Asked

Viewed 1,300 times

2

I have a data field (Datetime) and also an integer field. I need to make the difference between this date field and today’s date (Datetime.Now) and compare whether it is larger or smaller than my integer field. I made that mistake.

Oprerator '>' cannot be applied to operands of type 'System.TimeSpan' and 'int'

That is my expression:

var atendimento = db.T_CRM_StatusPDV
                              .Where(dt => (dt.DT_TransacaoV - DateTime.Now) > dt.DE_FrequenciaTrans)
  • This integer value represents what? hours, minutes, seconds, milliseconds?

  • are days, whole

1 answer

2


Still picking up the days of the TimeSpan:

var atendimento = db.T_CRM_StatusPDV
                  .Where(dt => ((TimeSpan)(dt.DT_TransacaoV - DateTime.Now)).Days > dt.DE_FrequenciaTrans)

Reference: http://msdn.microsoft.com/en-us/library/system.timespan.days.aspx

If the number of days allows fractional part, try TotalDays:

var atendimento = db.T_CRM_StatusPDV
                  .Where(dt => ((TimeSpan)(dt.DT_TransacaoV - DateTime.Now)).TotalDays > dt.DE_FrequenciaTrans)
  • @pnet I don’t think so. Here at this link says that TimeSpan belongs to System. I should have found.

  • @pnet I edited the answer. See if it now works for you.

  • @pnet I do not know if it has to do with the TimeSpan be null, but I did one more edit where a cast for the right type.

  • @pnet Anything, I think it’s cool you get the non-null value before applying the Where.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.