我写了下面的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 as
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案例,希望现在很清楚
答案 0 :(得分:0)
通过处理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();