简体   繁体   English

将“null”值赋给Nullable <DateTime> 单行“如果”

[英]Assigning `null` value to Nullable<DateTime> with single line 'if'

I have a Class like this 我有一个像这样的班级

public class MyClass
{
    public int Id { get; set; }
    public Nullable<DateTime> ApplicationDate { get; set; }
    ....
}

Now I'm trying to fill an object of MyClass like this 现在我正试图像这样填充MyClass的对象

DataTable dt = DBHelper.GetDataTable(sql, conn);
DataRow dr = dt.Rows[0];

MyClass oMyClass = new MyClass();
oMyClass.Id = (int)dr["Id"];
oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value ? null : Convert.ToDateTime(dr["AppDate"]); 
//Above line gives an error
....

Assigning of Application Date value gives an error 分配应用程序日期值会出错

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'

What am I missing here? 我在这里错过了什么?

You need to cast null to DateTime? 你需要将nullDateTime? :

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value 
    ? (DateTime?)null 
    : Convert.ToDateTime(dr["AppDate"]); 

This is because of the way the compiler determines the resulting type of the conditional operator ; 这是因为编译器确定条件运算符的结果类型的方式; the behavior is by design: 行为是设计的:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other. first_expression和second_expression的类型必须相同,或者从一种类型到另一种类型必须存在隐式转换。

Since null by itself is of null type and thus there is no conversion from or to it, you need to help the compiler by casting. 由于null本身是null类型 ,因此没有转换或来自它,你需要通过强制转换来帮助编译器。

oMyClass.ApplicationDate =
    dr["ApplDate"] == DBNull.Value ?
    (DateTime?)null :
    Convert.ToDateTime(dr["AppDate"]);

All the compiler knows is that one thing evaluates to a null and the other evaluates to a DateTime . 所有编译器都知道,一件事评估为null而另一件评估为DateTime The compiler complains because it can't convert from one to the other so it's up to you to cast them to something that can be both values. 编译器抱怨,因为它无法从一个转换为另一个,因此您可以将它们转换为可以同时为值的东西。

Note that DateTime? 注意DateTime? is short for Nullable<DateTime> . Nullable<DateTime>缩写。
Also note that you only need to cast the null value as there is an implicit conversion between DateTime? 另请注意,您只需要转换null值,因为DateTime?之间存在隐式转换DateTime? and DateTime so the compiler can do that conversion its self. DateTime所以编译器可以自己进行转换。

try this: 尝试这个:

oMyClass.ApplicationDate = 
    dr["ApplDate"] == DBNull.Value ? (DateTime?)null : 
                                     Convert.ToDateTime(dr["AppDate"]); 

You can also apply the cast to the last expression. 您还可以将强制转换应用于最后一个表达式。

You will need to convert "null" into Nullable 您需要将“null”转换为Nullable

Try this code: 试试这段代码:

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dr["AppDate"]);

You can use default which will assign the default value of an uninitialized Type. 您可以使用default值来指定未初始化类型的默认值。

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value ? default(Nullable<DateTime>) : Convert.ToDateTime(dr["AppDate"]); 

More examples 更多例子

bool isHappy = default(bool); //isHappy = false
int number = default(int); //number = zero
string text = default(text); // text = null
MyObject myObject = default(MyObject); // myObject = null
DateTime? date = default(DateTime?); //date = null
DateTime? dt = (DateTime?)null;

要么

Nullable<DateTime> dt = (Nullable<DateTime>)null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM