2015-10-20 24 views
1

我有許多由實體框架創建的關係。在有許多關係的表中創建LINQ

我嘗試從具有多種關係的表中檢索數據。

這裏的方案:

enter image description here

這裏是我的Linq創建:

var inspArchive = context 
    .Set<InspectionArchive>() 
    .Where(x => x.CustomerId == clientId && x.InspectionDomainId == domainId) 
    .Select(x=>x); 

的LINQ從inspectionArchives表中獲取記錄當條件滿足。

在上面的LINQ中,我需要再包含一個條件,考慮InspectionAuthoreties表的Id。

如何更改上面的LINQ以考慮InspectionAuthoreties表的Id。

+0

作爲其多對多所以具體是你想要的? 「檢驗存檔」後 –

回答

1

假設InspectionArchive實體有InspectionAuthority類型的集合導航屬性的InspectionArchive一部分,那麼你可以做此:

int inspectionAuthId=3; 
var inspArchive = context.Set<InspectionArchive>() 
         .Where(x => x.CustomerId == clientId 
            && x.InspectionDomainId == domainId 
            && x.InspectionAuthorities.Any(ia=>ia.Id==inspectionAuthId)); 
         //.Select(x=>x); you don't need this select 

另一種解決方案可能是使用查詢符號:

int inspectionAuthId=3; 
var inspArchive = from ia in context.Set<InspectionArchive>() 
        from i in ia.InspectionAuthorities 
        where ai.CustomerId == clientId 
         && ai.InspectionDomainId == domainId 
         && i.Id==inspectionAuthId 
        select ia; 
+0

爲什麼我不需要選擇? – Michael

+0

因爲你投射的是同一個實體,所以在第二個解決方案中由於查詢符號的語法是強制性的。 – octavioccl

1

通過from建立您的查詢InspectionAuthoreties inspectionArchives

你在那裏將根據

x => x.InspectionAuthoreties.CustomerId 

加入兩側並返回結果