Make sure those two types are nullable DateTime
var lastPostDate = reader[3] == DBNull.Value ?
null :
(DateTime?) Convert.ToDateTime(reader[3]);
DateTime?
instead of Nullable<DateTime>
is a time saver...I have found this excellent explanations in Eric Lippert blog:
The specification for the ?:
operator states the following:
The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then,
If X and Y are the same type, then this is the type of the conditional expression.
Otherwise, if an implicit conversion exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
Otherwise, if an implicit conversion exists from Y to X, but not from X to Y, then X is the type of the conditional expression.
Otherwise, no expression type can be determined, and a compile-time error occurs.
The compiler doesn't check what is the type that can "hold" those two types.
In this case:
null
and DateTime
aren't the same type.null
doesn't have an implicit conversion to DateTime
DateTime
doesn't have an implicit conversion to null
So we end up with a compile-time error.