tags:

views:

22

answers:

2

The below query fails with Null Reference Exception when there are elements in BOMIDs where MatID property is nothing. I thought the 'x.MatID isnot Nothing AndAlso' would prevent the x.MatID.Process part of the where from being executed. There are a couple elements in the BOMIDs collection that where MatID is nothing. Any thoughts?

From x In BOMIDs _
Group Join y As PurchasedProcess In SpecialProcesses _
On x.MatID.PurchasedProcess Equals y.Name _
Into G = Group _
From z In G.DefaultIfEmpty() _
Where x.MatID IsNot Nothing AndAlso _
x.MatID.Process = ProcessEnum.PurchasedProcess _
Select New With {.Item = x.Item, .Process = z}
+1  A: 

From glancing over your code, it seems that this line might be a potetntial trouble spot as well:

On x.MatID.PurchasedProcess Equals y.Name

Since x.MatID can be Nothing

chakrit
+2  A: 

AndAlso isn't the problem. Move your null check to just before the join to effectively filter out all null occurrences before joining on them.

Try this updated query:

From x In BOMIDs _
Where x.MatID IsNot Nothing _
Group Join y As PurchasedProcess In SpecialProcesses _
On x.MatID.PurchasedProcess Equals y.Name _
Into G = Group _
From z In G.DefaultIfEmpty() _
Where x.MatID.Process = ProcessEnum.PurchasedProcess _
Select New With {.Item = x.Item, .Process = z}
Ahmad Mageed
That works great, thanks. Still getting used to Linq, I think my head is stuck in SQL.
Kratz