使用 DefaultIfEmpty() 时的空引用异常
null reference exception on using DefaultIfEmpty()
我写了下面的 sql 查询
SELECT *
FROM dbo.PR_ADDRESS LEFT OUTER JOIN
dbo.Book_MarkText ON dbo.PR_ADDRESS.GroupID = dbo.Book_MarkText.BMData
where
dbo.PR_ADDRESS.GroupID = dbo.Book_MarkText.BMData
OR
PR_ADDRESS.GroupID in ('001','002','003')
LINQ 作为
string[] srGroupIDs = new string[] { "001", "002", "003" };
var objResult =
(from PR_ADDRESS in dtPR_ADDRESS.AsEnumerable()
join
Book_MarkText in dtBookmarkText.AsEnumerable() on
(string)PR_ADDRESS["GroupID"] equals (string)Book_MarkText["BMData"]
into Book_MarkText_join
from Book_MarkText_Temp in Book_MarkText_join.DefaultIfEmpty()
where srGroupIDs.Contains(PR_ADDRESS["Groupid"])
|| ((string)PR_ADDRESS["Groupid"] == (string)Book_MarkText_Temp["BMData"])
select new
{
ID = PR_ADDRESS["ID"],
Name1 = PR_ADDRESS["Name1"]
}).ToList();
但是在
行给我空引用异常
where srGroupIDs.Contains(PR_ADDRESS["Groupid"])
|| ((string)PR_ADDRESS ["Groupid"] == (string)Book_MarkText_Temp["BMData"])
为什么?
如果我删除 .DefaultIfEmpty()
,这毫无例外地工作,但我需要 'LEFT OUTER JOIN' 所以不能删除 .DefaultIfEmpty()
。我该如何解决?
编辑 :-
我也添加了 OR 大小写,希望现在清楚了
通过处理 Book_MarkText_Temp
null 作为 Jon Skeet 的评论解决。感谢您抽空帮助我。
string[] srGroupIDs = new string[] { "001", "002", "003" };
var objResult =
(from PR_ADDRESS in dtPR_ADDRESS.AsEnumerable()
join
Book_MarkText in dtBookmarkText.AsEnumerable() on
(string)PR_ADDRESS ["GroupID"] equals (string)Book_MarkText["BMData"] into Book_MarkText_join
from Book_MarkText_Temp in Book_MarkText_join.DefaultIfEmpty()
where Book_MarkText_Temp == null ? srGroupIDs.Contains(PR_ADDRESS["Groupid"]) :
srGroupIDs.Contains(PR_ADDRESS["Groupid"])
|| ((string)PR_ADDRESS["Groupid"] == (string)Book_MarkText_Temp["BMData"])
select new {
ID = PR_ADDRESS ["ID"],
Name1 = PR_ADDRESS ["Name1"]
}).ToList();
我写了下面的 sql 查询
SELECT *
FROM dbo.PR_ADDRESS LEFT OUTER JOIN
dbo.Book_MarkText ON dbo.PR_ADDRESS.GroupID = dbo.Book_MarkText.BMData
where
dbo.PR_ADDRESS.GroupID = dbo.Book_MarkText.BMData
OR
PR_ADDRESS.GroupID in ('001','002','003')
LINQ 作为
string[] srGroupIDs = new string[] { "001", "002", "003" };
var objResult =
(from PR_ADDRESS in dtPR_ADDRESS.AsEnumerable()
join
Book_MarkText in dtBookmarkText.AsEnumerable() on
(string)PR_ADDRESS["GroupID"] equals (string)Book_MarkText["BMData"]
into Book_MarkText_join
from Book_MarkText_Temp in Book_MarkText_join.DefaultIfEmpty()
where srGroupIDs.Contains(PR_ADDRESS["Groupid"])
|| ((string)PR_ADDRESS["Groupid"] == (string)Book_MarkText_Temp["BMData"])
select new
{
ID = PR_ADDRESS["ID"],
Name1 = PR_ADDRESS["Name1"]
}).ToList();
但是在
行给我空引用异常 where srGroupIDs.Contains(PR_ADDRESS["Groupid"])
|| ((string)PR_ADDRESS ["Groupid"] == (string)Book_MarkText_Temp["BMData"])
为什么?
如果我删除 .DefaultIfEmpty()
,这毫无例外地工作,但我需要 'LEFT OUTER JOIN' 所以不能删除 .DefaultIfEmpty()
。我该如何解决?
编辑 :-
我也添加了 OR 大小写,希望现在清楚了
通过处理 Book_MarkText_Temp
null 作为 Jon Skeet 的评论解决。感谢您抽空帮助我。
string[] srGroupIDs = new string[] { "001", "002", "003" };
var objResult =
(from PR_ADDRESS in dtPR_ADDRESS.AsEnumerable()
join
Book_MarkText in dtBookmarkText.AsEnumerable() on
(string)PR_ADDRESS ["GroupID"] equals (string)Book_MarkText["BMData"] into Book_MarkText_join
from Book_MarkText_Temp in Book_MarkText_join.DefaultIfEmpty()
where Book_MarkText_Temp == null ? srGroupIDs.Contains(PR_ADDRESS["Groupid"]) :
srGroupIDs.Contains(PR_ADDRESS["Groupid"])
|| ((string)PR_ADDRESS["Groupid"] == (string)Book_MarkText_Temp["BMData"])
select new {
ID = PR_ADDRESS ["ID"],
Name1 = PR_ADDRESS ["Name1"]
}).ToList();