这是我的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 to 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子句中引用左外连接表?请帮忙!
答案 0 :(得分:0)
据我所知,这一行
from g1 in group1.Where(fA => fA.Year == reportYear).DefaultIfEmpty()
如果没有行,可以生成null
。在这种情况下,当您尝试对其进行过滤(where g1.sourceName == null
)时,您将获得空引用(因为g1
为null
)。