0

这是我的 SQL 查询:

Select  <a bunch of columns>
from fields f
join Table1 c1 on c1.fieldID = f.ID and c1.year = 2014
join Table2 fRes on f.ID = fRes.fieldID
join Table3 stst on f.ID = stst.fieldID
join Table4 model on c1.ID = model.yearID
left join Table5 fA on f.ID = fA.fieldID and fA.year = 2014 
left join Table6 nA on f.ID = nA.fieldID and nA.year = 2014 
where  fA.sourcename IS NULL and nA.Sourcename IS NULL

LINQ 到 SQL:

from f in BasicDataAccess.Data.FieldsList
join c1 in BasicDataAccess.Data.Table1List on f.ID equals c1.fieldID 
join fRes in BasicDataAccess.Data.Table2List on f.ID equals fRes.fieldID
join soilTst in BasicDataAccess.Data.Table3List on f.ID equals soilTst.fieldID
join modelRsNM in BasicDataAccess.Data.Table4List on c1.ID equals modelRsNM.YearID
join fA in BasicDataAccess.Data.Table5List on f.ID equals fA.fieldID into group1
from g1 in group1.Where(fA => fA.Year == reportYear).DefaultIfEmpty()
join nA in BasicDataAccess.Data.Table6List on f.ID equals nA.fieldID into group2
from g2 in group2.Where(nA => nA.Year == reportYear).DefaultIfEmpty()
where g1.sourceName == null && g2.sourceName == null

问题:执行时查询抛出 Nullreference 异常未处理..对象引用未设置为对象错误的实例..

我错过了什么?是否可以在 where 子句中引用左外连接表?请帮忙!

4

1 回答 1

0

据我所知,这条线

from g1 in group1.Where(fA => fA.Year == reportYear).DefaultIfEmpty()

null如果没有行可以生产。在这种情况下,当您尝试对其进行过滤时(where g1.sourceName == null),您将获得空引用(因为g1is null)。

于 2012-07-24T20:39:51.373 回答