我在语句中使用了多个连接,并尝试对此进行linq-to-SQl查询,但没有成功。
SELECT ur.UserName, ur.LandmarkRef, lt.Date, l.Place
FROM
tbl_Users ur
LEFT OUTER JOIN tbl_LandMarks l ON ur.LandmarkRef = l.LandMarkID
INNER JOIN tbl_LandmarkTypes lt ON l.LandmarkTypeRef equals lt.LandmarkTypeID
WHERE ur.UserId == pUserId
以下是Linq-to-SQL查询,如果我使用LEFT OUTER JOIN则不起作用
UserDBDataContext lDc = new UserDBDataContext();
var lU= (from ur in lDc.tbl_Users
join l in lDc.tbl_LandMarks on ur.LandmarkRef equals l.LandMarkID into Lmark
from lmk in Lmark.DefaultIfEmpty()
join lt in lDc.tbl_LandmarkTypes on lmk.LandmarkTypeRef equals lt.LandmarkTypeID
where ur.UserId == pUserId
select new { ur.UserName, ur.LandmarkRef, lc.Date, lmk.Place});
这里Landmark实际上是一个可选字段,因此它可能是NULL,所以在这种情况下,由于Landmark中的NULL值,LEFT join不起作用并且不返回任何行。它适用于SQL但不适用于LINQ-to-SQL。