我試圖重新在LINQ這個SQL查詢:LINQ子查詢多列
SELECT *
FROM Policies
WHERE PolicyID IN(SELECT PolicyID
FROM PolicyRegister
WHERE PolicyRegister.StaffNumber = @CurrentUserStaffNo
AND (PolicyRegister.IsPolicyAccepted = 0
OR PolicyRegister.IsPolicyAccepted IS NULL))
關係圖的兩個表:
這裏是我的嘗試至今:
var staffNumber = GetStaffNumber();
var policyRegisterIds = db.PolicyRegisters
.Where(pr => pr.StaffNumber == staffNumber && (pr.IsPolicyAccepted == false || pr.IsPolicyAccepted == null))
.Select(pr => pr.PolicyID)
.ToList();
var policies = db.Policies.Where(p => p.PolicyID.//Appears in PolicyRegisterIdsList)
我想我很接近,可能會做兩個列表並使用Inters ect()不知何故,但我今天早上看了我的代碼,並認爲必須有一個更簡單的方法來做到這一點。 LINQ應該是一個更可讀的數據庫語言吧?
提供的任何幫助非常感謝。
db.Policies.Where(P => policyRegisterIds。載有(p.PolicyID)) – jitender
嘗試'policyRegisterIds.Contains(p.PolicyID)',但最好儘量減少請求的數量 – ASpirin
你在'Policies'和'PolicyRegisters'之間有一個虛擬關係嗎?包含問題 – meorfi