2011-04-06 137 views
0

我非常新手使用LINQ,但我有幾個複雜的SQL查詢轉換爲LINQ的,所以我買Linqer的副本。不幸的是,我知道有一個SQL查詢Linqer無法轉換爲Linq,因爲Linqer無法將SQL轉換爲子連接。我希望有人能幫助我的LINQ的代碼,這個SQL查詢:LINQ查詢具有多個左加入

SELECT v.*, rci.CustomId, ci.EntryTime As CheckInTime, ci.Operator As CheckInOperator, 
    cis.Name As CheckInStation, co.EntryTime As CheckOutTime, co.Operator As CheckOutOperator, 
    cos.Name As CheckOutStation, cat.Name As Category, clr.Name As Clearance, r.ReasonForVisit As Reason, 
    s.SiteId + ' -- ' + s.SiteName As Site, e.LastName + ', ' + e.FirstName As Employee 
    FROM ((((((((((Visitor v LEFT JOIN VisitorEntry ci ON v.CheckInId = ci.Id) 
    LEFT JOIN VisitorEntry co ON v.CheckOutId = co.Id) 
    LEFT JOIN Station cis ON ci.StationId = cis.Id) 
    LEFT JOIN Station cos ON co.StationId = cos.Id) 
    LEFT JOIN Category cat ON v.CategoryId = cat.Id) 
    LEFT JOIN Clearance clr ON v.ClearanceId = clr.Id) 
    LEFT JOIN Reason r ON v.ReasonId = r.Id) 
    LEFT JOIN Site s ON v.SiteId = s.Id) 
    LEFT JOIN Employee e ON v.EmployeeId = e.Id) 
    LEFT JOIN RecordCustomId rci ON v.Id = rci.ParentId) 
    WHERE 1=1 

(我有類似這樣的幾個SQL查詢,我需要轉換成的LINQ所以這就是爲什麼我有「WHERE」佔位符的查詢)

此外,我一直在看這篇博客,但我仍然無法理解如何將我的查詢翻譯: http://codingsense.wordpress.com/2009/06/16/multiple-list-left-join-in-linq/

謝謝! Mike

回答

2

SQL中所有括號的含義是什麼?它們似乎沒有做任何事情,如果它們被刪除,您得不到完全相同的結果嗎?您使用DefaultIfEmpty方法在linq中執行左連接。我認爲你正在使用LinqToSql?下面從最近的一個項目一個例子,我有

return from app in pi_GetApplications() 
     from names in app.tContact.tNames // Inner Join 
     from addr in app.tContact.tAddresses.DefaultIfEmpty() // Left Outer Join 
     select app; 

工作這裏假設你有在您的DBML文件中定義,這樣LinqtoSql知道實體是如何關聯的外鍵關聯。