frenchie
frenchie

Reputation: 51917

linq with nullable datetime

I'm running a linq-to-sql query on a table that contains a nullable datetime like this:

 var Output = from s in ....
              select new MyModel() 
              {
                  TheCloseDate = s.TheCloseDate.DateTime.Value
              }

However, the variable TheCloseDate never gets filled. The MyModel definition of the variable is :

public Nullable<DateTime> TheCloseDate { get; set; }

What am I doing wrong here?

Thanks for your suggestions.

Upvotes: 3

Views: 494

Answers (1)

Jason Evans
Jason Evans

Reputation: 29186

Try

var Output = from s in ....
              select new MyModel() 
              {
                  TheCloseDate = s.TheCloseDate ?? null // Or whatever you want, like DateTime.Now
              }

Upvotes: 4

Related Questions