在F#</datetime>中将DateTime分配给Nullable <datetime>

时间:2013-06-24 10:45:03

标签: casting f# type-conversion nullable

我有一些F#代码,我需要更新数据库记录中的字段。我正在使用Type Provider for SQL。该表具有可空的日期值字段。当我尝试将date字段的值更新为DateTime.UtcNow时,编译器会抱怨“此表达式应该具有Nullable类型&lt; DateTime&gt;但这里的类型为DateTime”。如何将DateTime转换/转换为Nullable&lt; DateTime&gt;。

我的代码目前看起来如下:

for queryItem in queryResult do
    queryItem.CurrentDate <-  DateTime.UtcNow  // This gives compiler error as described above

此致

1 个答案:

答案 0 :(得分:13)

John Palmer的评论上面是解决方案的链接。需要使用System.Nullable构造函数,即

queryItem.CurrentDate <- System.Nullable DateTime.UtcNow