0

我有以下代码:

var distinctRows = 
      (from row in ds.GetTable(this.TagShortName).AsEnumerable()
         select new Audit    
          {
             ID =row.Field<long>("ID"),
             UpdatedBy =string.Format("{0} {1}", 
                                      row.Field<string>("UpdatedByFirstName"),
                                      row.Field<string>("UpdatedByLastName")),
             UpdatedOn = ow.Field<DateTime>("UpdatedOn")
          }).Distinct(new AuditComparer());

当我在 UpdateOn 字段中有空值时,它会抛出异常:

UpdatedOn =row.Field<DateTime>("UpdatedOn")

异常:无法将 DBNull.Value 转换为类型“System.DateTime”。请使用可为空的类型。

如何处理?

4

2 回答 2

1

使UpdatedOn可空或

UpdatedOn = row.Field<DateTime?>("UpdatedOn")?? DateTime.MinValue
于 2013-04-11T08:59:59.703 回答
0

使用Nullable<DateTime>DateTime?

UpdatedOn = row.Field<DateTime?>("UpdatedOn")
于 2013-04-11T08:55:06.020 回答