System.Nullable<System.TimeSpan>' does not contain a definition for 'TotalMinutes'

c#, c#-4.0, datetime, nullable, timespan

Solution

Personally I'd write this as:

if (entity.End > DateTime.Now.AddMinutes(1))
{
    ...
}

That condition will be false if `entity.End` is null.

I often find that rather than one date/time from another and comparing that with a `TimeSpan` (or whatever type you're using), it's clearer to compute a lower-bound or upper-bound, and compare that with the variable date/time. It certainly works out cleanly in this case.

EDIT: Okay, now that the question's a little clearer, I'd write this as:

DateTime now = DateTime.UtcNow;
DateTime lowerBound = new DateTime(now.Year, now.Month, now.Hour, now.Minute,
                                   0, 0, DateTimeKind.Utc);
DateTime upperBound = now.AddMinutes(1);
if (entity.End >= lowerBound && entity.End < upperBound)
{
    ...
}

I suspect I may still have misunderstood though...

Problem

Is there an elegant way around this error rather than checking .HasValue in the if() then adding the Value such as DateTimeUtcNow.Value? ``` // Compare nullable date time entity.End with utcnow but only compare up to minute precision // for time. // if ((int)(entity.End - DateTime.UtcNow).TotalMinutes < 0) { // ASSERT: entity.End < UTCNow (i.e. 12/4/2012 3:56 PM < 12/4/2012 3:57 PM) } ``` Error: System.Nullable' does not contain a definition for 'TotalMinutes'

Original source