而且在VB Linq查询中,似乎并没有起作用

时间:2010-09-14 17:18:56

标签: .net vb.net linq

当BOMID中有MatID属性为空的元素时,以下查询失败并显示Null Reference Exception。 我认为'x.MatID is not Nothing AndAlso'会阻止x.MatID.Process部分执行。在BOMIDs集合中有几个元素,其中MatID是什么都没有。 有什么想法吗?

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}

2 个答案:

答案 0 :(得分:3)

AndAlso不是问题。将空检查移到联接之前,以便在加入之前有效地过滤掉所有出现的空白。

尝试此更新的查询:

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}

答案 1 :(得分:2)

从浏览代码看,这条线似乎也可能是一个潜在的问题点:

On x.MatID.PurchasedProcess Equals y.Name

由于x.MatID可以是Nothing