On Oct 19, 10:33*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sun, 19 Oct 2008 14:03:00 -0700, shapper <mdmo...@gmail.com> wrote:
> > Hello,
>
> > I have the following:
>
> > DateTime c = database.Posts.Max(b => b.CreatedAt).Value;
>
> > b.CreatedAt is of type "DateTime?"
>
> > The problem is when database.Posts is empty. I get an error:
> > Nullable object must have a value.
>
> > I understand that I get a null value but I am not able to apply ??
> > because it does not allow me.
>
> > How can I solve this?
>
> Can you be more explicit about what you mean by "it does not allow me"? *
> You didn't post any code that tries to use the null coalescing operator *
> (??). *There's not any particular reason why "it" (the compiler, I presume *
> you mean) should not "allow" you to use it.
>
> If you can post the code that you think should work but doesn't, we can *
> help change that to code that does work.
>
> Pete
Sure ... Sorry. I tried:
DateTime c = database.Boxes.Max(b => b.CreatedAt ?? DateTime.UtcNow);
Which seemed logic to me but I get:
The null value cannot be assigned to a member with type
System.DateTime which is a non-nullable value type.
I also tried:
DateTime c = database.Boxes.Max(b => b.CreatedAt).Value ??
DateTime.UtcNow;
But this one does not even compile and I get:
Operator '??' cannot be applied to operands of type 'System.DateTime'
and 'System.DateTime
Thanks,
Miguel
|