【发布时间】:2011-07-20 03:58:08
【问题描述】:
可能的重复:
Why does null need an explicit type cast here?
Nullable types and the ternary operator. Why won't this work?
尝试执行以下操作:
sqlCmd.Parameters.Add("@DateCreated", System.Data.SqlDbType.DateTime).Value
= myObject.DateCreated == DateTime.MinValue
? DBNull.Value : myObject.DateCreated;
我收到此错误:
条件表达式的类型不能 确定,因为没有 之间的隐式转换 'System.DBNull' 和 'System.DateTime'
我显然理解了这个错误,但是考虑到 Parameters.Value 是对象类型,为什么类型甚至很重要?有没有办法完成我想做的事情?
【问题讨论】:
-
非常相关:stackoverflow.com/questions/2608878 - 事实上,我认为它们是重复的,因为它们的答案完全相同。简而言之:只需将其中一个投射到
Object -
这是几十个问题的重复。喜欢这个:stackoverflow.com/questions/858080 和这个:stackoverflow.com/questions/2450866 和这个:stackoverflow.com/questions/75746
标签: c# sql types type-conversion ternary-operator