0

我有以下代码:

Dim getProspect = (From p In dbContext.IRF_Prospects _
                   Where p.url = prospect_url _
                   Select p).FirstOrDefault
' If they have a record...
If Not IsDBNull(getProspect) Then
    If IsDBNull(getProspect.user_id) Then
        ' Prepopulate the form with their information.
        txtFirst.Text = getProspect.first_name
Else
        ' Redirect them to login.
        Response.Redirect("login.aspx")
End If

当我执行它时,它会抛出一个未设置为对象错误实例的对象引用getProspect.user_id。为什么要这样做?我IsDBNull首先验证它是否存在的事实不应该防止这种情况发生吗?

4

1 回答 1

1

DBNullNothing和你所拥有的不一样NothingFirstOrDefault,顾名思义,返回第一项或默认值,Nothing用于引用类型 - never DBNull

Dim getProspect = (From p In dbContext.IRF_Prospects _
                   Where p.url = prospect_url _
                   Select p).FirstOrDefault
' If they have a record...
If getProspect IsNot Nothing Then
    If IsDBNull(getProspect.user_id) Then
        ' Prepopulate the form with their information.
        txtFirst.Text = getProspect.first_name
    Else
        ' Redirect them to login.
        Response.Redirect("login.aspx")
    End If
于 2012-05-22T23:15:22.923 回答