Linq 多条件查询DATATABLE数据时,报Linq Exception(NullReferenceException)错误

来源:互联网 发布:如何申请淘宝图片保护 编辑:程序博客网 时间:2024/06/05 23:46

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/  

 


原创粉丝点击