dereference DateTime? to class.DateTime

时间:2012-08-24 02:25:46

标签: c# linq

我在这方面得到一个空引用异常,我不知道为什么。

if (units.Min(sd => sd.MONTH_UNIT_APPLIES_TO) != null)
{
    DateTime? dt = (DateTime)units.Min(sd => sd.MONTH_UNIT_APPLIES_TO);
    // dt = {8/1/2012 12:00:00 AM}
    crctw.unitChecks.startDate = (DateTime)dt;  // // NullReferenceException here
}

开始日期字段位于此包装类中:

public class checksWrapper
{
    public DateTime startDate { get; set; }
    ...
}

由于它是一个值类型,我认为我不应该使用new运算符。

如果调试器说它的日期为{8/1/2012 12:00:00 AM}那么为什么我会得到一个空引用?

1 个答案:

答案 0 :(得分:3)

crctwunitChecks为空。变量dt很好。你只是看着= ...

的错误一面