在LINQ中传递Null日期值

时间:2013-03-28 20:55:15

标签: linq linq-to-sql linq-to-dataset

    Private m_ExpirationDate As Nullable(Of DateTime)
Public Property ExpirationDate() As Nullable(Of DateTime)
    Get
        Return m_ExpirationDate
    End Get
    Set(ByVal value As Nullable(Of DateTime))
        m_ExpirationDate = value
    End Set
End Property


Dim rec As New DBML.Staff With _
        {.StaffDesc = m_StaffDesc, _
         .LastName = m_LastName, _
         .FirstName = m_FirstName, _
         .MiddleInit = m_MiddleInit, _
         .Gender = m_Gender, _
         .Certification = m_Certification, _
         .ExpirationDate = m_ExpirationDate, _ ********
         .Active = m_Active, _
         .CEPDNo = m_CEPDNo, _
         .FaNo = m_FaNo, _
         .PIC = m_PIC, _
         .PICValid = m_PICValid, _
         .PICCheckDate = Today(), _
         .PICError = m_PICError}

    _db.Staffs.InsertOnSubmit(rec)

    Try
        _db.SubmitChanges()
        RetVal = rec.StaffID.ToString
    Catch exp As Exception
        RetVal = "Insert Failed:" & exp.Message

    End Try

当我运行insertonsubmit时,它将失败,因为m_ExpirationDate的值为null,尽管它显示在12:00:00:00AM。那么我该如何测试这个值,如果它为null,则不要在“InsertOnSubmit”语句中包含它。我不想传递像1/1/1900这样的虚假价值。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

    Private m_ExpirationDate As Nullable(Of DateTime)
Public Property ExpirationDate() As Nullable(Of DateTime)
    Get
        Return m_ExpirationDate
    End Get
    Set(ByVal value As Nullable(Of DateTime))
        If Not IsDate(value) Or value = #12:00:00 AM# Then
            m_ExpirationDate = Nothing
        Else
            m_ExpirationDate = value
        End If

    End Set
End Property

我需要做的是将我的属性值设置为“无”,如果它是空白值或不是日期。