2010-09-25 126 views
1

我在一個語句中使用多個聯接,並試圖使linq-to-SQl查詢,但沒有成功。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到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}); 

不工作在這裏置地實際上是一個可選字段,因此它可能爲空,因此在這種情況下LEFT由於Landmark中的NULL值,連接不起作用並且不返回任何行。它在SQL中工作,但不在LINQ-to-SQL中。

回答