C# GetData包含时间为空

C# GetData包含时间为空,c#,C#,我用这个代码从数据库中获取数据 var table = kantarDataSetTartimlarTableAdapter.GetData().Select(s => new { s.DateColumn, s.Index }).AsEnumerable().Select ((s, column) => n

我用这个代码从数据库中获取数据

var table = kantarDataSetTartimlarTableAdapter.GetData().Select(s =>  new
                    {
                        s.DateColumn,
                        s.Index
                    }).AsEnumerable().Select ((s, column) => new
                                          {                                                  
                                              s.DateColumn,                                                  
                                              s.Index                                                  
                                              column_no = column + 1
                                          });
如果
date列
不是
null
我没有任何问题。但是当
date列
null
数据时,我遇到了一个问题:

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public System.DateTime event_start_date {
get {
    try {
        return ((global::System.DateTime)(this[this.tableDataTable1.event_start_dateColumn]));
    }
    catch (global::System.InvalidCastException e) {
        throw new global::System.Data.StrongTypingException("The value for column \'event_start_date\' in table \'DataTable1\' is DBNull.", e);
    }
}
set {
    this[this.tableDataTable1.event_start_dateColumn] = value;
}
}


如何解决此错误?

您的DB列和实体模型似乎不同步。如果从数据库中获取
null
值,则该字段必须为空。为了将其映射到您的模型,它还必须支持可为空的日期


您需要在模型中更新
event\u start\u date
以使用
Nullable
/
DateTime?

从数据库读取值时,您可以尝试提供默认值,以确保不存储任何空值:

DateColumn = s.DateColumn ?? DateTime.MinValue

我更新
事件\u开始\u日期
并解决我的问题

get {
                try {
                    if (this[this.table.DateTimeColumn] is DBNull)
                    {
                        return Convert.ToDateTime(null);
                    }
                    else
                    {
                        return ((global::System.DateTime)(this[this.table.DateTimeColumn]));
                    }
                }
                catch (global::System.InvalidCastException e) { 
                    throw new global::System.Data.StrongTypingException("Description", e);                        
                }
            }

            set {
                this[this.table.DateTimeColumn] = value;
            }