PC Review


Reply
Thread Tools Rate Thread

DateTime Null

 
 
shapper
Guest
Posts: n/a
 
      19th Oct 2008
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?

Thanks,
Miguel

 
Reply With Quote
 
 
 
 
shapper
Guest
Posts: n/a
 
      19th Oct 2008
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
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      20th Oct 2008
On Oct 20, 12:20*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sun, 19 Oct 2008 14:43:30 -0700, shapper <mdmo...@gmail.com> wrote:
> > 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.

>
> Ah. *Yes, it would do that when the database is empty. *But, if you move *
> the coalescing operator out of the lambda expression, I think it should be *
> okay:
>
> * * *DateTime c = database.Boxes.Max(b => b.CreatedAt) ?? DateTime.UtcNow;
>
> Note that that's subtly different from what you did try:
>
> > I also tried:
> > DateTime c = database.Boxes.Max(b => b.CreatedAt).Value ??
> > DateTime.UtcNow;

>
> You can only use nullable types with the coalescing operator. *The *
> Nullable<DateTime>.Value property is a non-nullable value type and so is *
> an illegal operand for the operator.
>
> Without a true concise-but-complete code sample, it's difficult to know *
> for sure what will or won't work. *But I'm pretty sure the above should*
> solve your problem.
>
> Pete


Thank You Pete. I didn't know that about value ... I though it would
still be a nullable DateTime.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime and NULL value =?Utf-8?B?RmlsaXAgRGUgQmFja2Vy?= Microsoft Dot NET Framework 1 21st May 2006 03:04 PM
(Sql)DateTime and Null value Matt Jensen Microsoft C# .NET 1 11th Oct 2005 11:23 AM
Null and DateTime tshad Microsoft C# .NET 8 29th Sep 2005 08:14 AM
Set DateTime value to null news.microsoft.com Microsoft ADO .NET 9 20th Jul 2004 07:03 PM
Set DateTime value to null news.microsoft.com Microsoft C# .NET 9 20th Jul 2004 07:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:57 AM.