TimeSpan

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    TimeSpan

    Hello,

    I have the following:

    boxStat = new BoxStat {
    BoxCount = database.Boxes. Count(),
    SinceLastCreate = (DateTime.Now - database.Boxes. Max(b =>
    b.CreatedAt)).D ays ?? 0
    };

    I am getting an error:
    'System.Nullabl e<System.TimeSp an>' does not contain a definition for
    'Days' and no extension method 'Days' accepting a first argument of
    type 'System.Nullabl e<System.TimeSp an>' could be found (are you
    missing a using directive or an assembly reference?)

    I added ?? 0 to try to solve the problem but I keep having the same
    error.

    What am I doing wrong?

    Thank You,
    Miguel
  • gerry

    #2
    Re: TimeSpan

    For whatever reason , DateTime math has always seemed to require an explicit
    cast to TimeSpan :

    SinceLastCreate = ( (TimeSpan) (DateTime.Now - database.Boxes. Max(b
    =b.CreatedAt)) ).Days


    "shapper" <mdmoura@gmail. comwrote in message
    news:85d99938-52b1-448a-ab39-47e28cb62f55@p4 9g2000hsd.googl egroups.com...
    Hello,
    >
    I have the following:
    >
    boxStat = new BoxStat {
    BoxCount = database.Boxes. Count(),
    SinceLastCreate = ((TimeSpan)(Dat eTime.Now - database.Boxes. Max(b
    =>
    b.CreatedAt))). Days ?? 0
    };
    >
    I am getting an error:
    'System.Nullabl e<System.TimeSp an>' does not contain a definition for
    'Days' and no extension method 'Days' accepting a first argument of
    type 'System.Nullabl e<System.TimeSp an>' could be found (are you
    missing a using directive or an assembly reference?)
    >
    I added ?? 0 to try to solve the problem but I keep having the same
    error.
    >
    What am I doing wrong?
    >
    Thank You,
    Miguel

    Comment

    • Hans Kesting

      #3
      Re: TimeSpan

      shapper has brought this to us :
      Hello,
      >
      I have the following:
      >
      boxStat = new BoxStat {
      BoxCount = database.Boxes. Count(),
      SinceLastCreate = (DateTime.Now - database.Boxes. Max(b =>
      b.CreatedAt)).D ays ?? 0
      };
      >
      I am getting an error:
      'System.Nullabl e<System.TimeSp an>' does not contain a definition for
      'Days' and no extension method 'Days' accepting a first argument of
      type 'System.Nullabl e<System.TimeSp an>' could be found (are you
      missing a using directive or an assembly reference?)
      >
      I added ?? 0 to try to solve the problem but I keep having the same
      error.
      >
      What am I doing wrong?
      >
      Thank You,
      Miguel
      What does "database.Boxes .Max()" return? I'm guessing a nullable
      DateTime (either "DateTime?" or "Nullable<DateT ime>", they are the
      same).

      That means that that difference will not return a TimeSpan, but a
      nullable TimeSpan.
      That "TimeSpan?" does not have a "Days" property, but it has a Value
      property that returns a TimeSpan. That *does* have a Days property.
      (DateTime.Now - database.Boxes. Max(b =b.CreatedAt)). Value.Days

      Note: this fails if that Max returns a null!


      You could have used that ?? operator, but then at the point where a
      nullable type was involved:
      ((DateTime.Now - database.Boxes. Max(b =b.CreatedAt)) ??
      TimeSpan.Zero). Days
      This way you get 0 days when Max returned null.

      Or (as gerry suggested) cast to TimeSpan before reading the Days
      property. But this also fails when Max returns null.

      Hans Kesting


      Comment

      Working...