Type of conditional expression cannot be determined because there is no implicit conversion between ” and ‘System.DateTime’

I had the error contained in the title where I was trying to assign a DateTime to an object property that could be nullable. If the value is not null I had to parse the value as a DateTime… The code is as following:

results.Add(new MyObject() {
    ID = new Guid(row["MyObjectID"].ToString()),
    Title = row["Title"].ToString(),
    DateDisabled = row.IsNull("DateDisabled") ? null

     : DateTime.Parse(row["DateDisabled"].ToString()),
});

The easiest way to solve this is as follows:

results.Add(new MyObject() {
    ID = new Guid(row["MyObjectID"].ToString()),
    Title = row["Title"].ToString(),
    DateDisabled = row.IsNull("DateDisabled") ? (DateTime?)null
     : DateTime.Parse(row["DateDisabled"].ToString()),
});

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: