Linq 代码中where下有多个条件,执行时报此错误:
Linq Exception(NullReferenceException): Object reference not set to an instance of an object
With NullReferenceException:
GOOGLE后得到解决方案如下:
old code -->
Dim obj As DemographicProperties = Me
Dim m As IEnumerable(Of DemographicProperty) = From c In obj _
Where (c.MapClass.MapClassID = MapClassID) _
Order By c.Name Ascending _
Select c
new code -->
If you are getting NullReferenceException, it means the MapClassID is null(nothing in vb.net)
Solve:
Dim obj As DemographicProperties = Me
Dim m As IEnumerable(Of DemographicProperty) = From c In obj _
Where ((Not c.MapClass Is Nothing) AndAlso (c.MapClass.MapClassID = MapClassID)) _
Order By c.Name Ascending _
Select c
reference:
http://scripting.chaindb.com/2010/09/24/linq-exceptionnullreferenceexception-object-reference-not-set-to-an-instance-of-an-object/