0
我是新來的linq,並希望執行跨三個表的連接。在SQL相當於什麼,我試圖做的是:Linq左外連接跨多個表檢索每個表的信息
SELECT PM.Id, PM.new_clientId, .....
C.new_firstname, c.new_surname,.....
A.new_addressid, A.new_addressline1, A.new_addressline2.....
CY.new_county
FROM partialMatch PM
INNER JOIN new_client C ON PM.new_clientId = C.new_clientId
LEFT OUTER JOIN new_address A ON C.new_addressId = A.new_addressId
LEFT OUTER JOIN new_county CY ON A.new_countyId = CY.new_countyId
WHERE PM.ownerId = @UserId AND PM.new_reviewed <> true
我已經開發了LINQ的代碼是這樣的,但這些似乎並沒有被外連接,因爲它沒有返回任何結果,除非我註釋掉加入到地址和縣級表
var partialMatches = from pm in ContextService.CreateQuery<new_partialmatch>()
join c in ContextService.CreateQuery<new_client>() on pm.new_ClientId.Id equals c.new_clientId
join a in ContextService.CreateQuery<new_address>() on c.new_AddressID.Id equals a.new_addressId
join cy in ContextService.CreateQuery<new_county>() on a.new_CountyID.Id equals cy.new_countyId
where pm.OwnerId.Id == _currentUserId && pm.new_Reviewed != true
select new
{
Id = pm.Id,
new_ClientID = pm.new_ClientId,
new_MatchingCRMClientID = pm.new_MatchingCRMClientId,
new_MatchingVulcanClientID = pm.new_MatchingVulcanClientID,
new_name = pm.new_name,
firstname = c.new_FirstName,
surname = c.new_Surname,
dob = c.new_DateOfBirth,
addressId = a.new_addressId,
address1 = a.new_AddressLine1,
address2 = a.new_AddressLine2,
address3 = a.new_AddressLine3,
address4 = a.new_AddressLine4,
county = cy.new_County
};
任何幫助將不勝感激,
感謝, 尼爾
編輯:
我也試過使用'進入'的聲明,但後來在我的第二次加入別名是不承認。
from pm in ContextService.CreateQuery<new_partialmatch>()
join c in ContextService.CreateQuery<new_client>() on pm.new_ClientId.Id equals c.new_clientId into pmc
from x in pmc.DefaultIfEmpty()
join a in ContextService.CreateQuery<new_address>() on c.new_AddressID.Id equals a.new_addressId
所以對於c.newIddressID.Id等於a.new_addressId我收到此錯誤信息:
名「C」是不是在範圍上的「等於」左側。考慮交換'equals'兩邊的表達式。
謝謝傑夫,我試過這個,它編譯正確,但是當我加載頁面時,我得到這個消息:'SelectMany'操作必須先由一個實體ID過濾的'Where'操作。 – Neil
@ Neil我認爲問題是由左外連接上的左外連接引起的。我修改了一些查詢。 – Jeff