我有这样的查询:
Dim clcexists = (From p In dbContext.Signatures _
Where p.StudentID = people_id _
And p.ContractType = "clc" _
Order By p.ID Descending _
Select p)
稍后我使用IsNothing检查clcexists中是否存在任何行:
If IsNothing(clcexists) Then ' If no CLC is on file.
clcfirst = Date.Now.Subtract(year)
clcdate = clcfirst
Else ' If CLC is on file.
clcfirst = clcexists.FirstOrDefault()
clcdate = clcfirst.SignatureDate
End If
但是IsNothing()没有像我期望的那样运作。结果表中没有行,但它仍然表现得好像存在并转到Else子句。帮助
答案 0 :(得分:3)
您可能希望改为使用Any
:
If Not clcexists.Any() Then ' no CLC is on file.
clcfirst = Date.Now.Subtract(year)
clcdate = clcfirst
Else ' If CLC is on file.
clcfirst = clcexists.FirstOrDefault()
clcdate = clcfirst.SignatureDate
End If
答案 1 :(得分:2)
IsNothing
检查引用是否分配了对象实例(即,它不是null
)。在您的情况下,对象不是null
,因为它指向没有行的实例。
http://msdn.microsoft.com/en-us/library/5adx7fxz(v=vs.71).aspx
您应该使用Rows.Count
,Rows.Any()
或HasRows
属性来检查您的实例上是否存在行。
答案 2 :(得分:1)