2010-04-21 122 views
0

請原諒我的問題的上下文,因爲我不知道如何正確地對它進行說明。LINQ - 查詢通過多對多關係篩選的列表

爲了不更復雜的是,這裏是我的業務需求:「我帶回來,他們在部門所屬的所有員工‘X’

所以,當我看到這一點,它會顯示所有屬於僱員的到這個部門

這裏是我的環境:Silverlight 3與實體框架1.0和WCF數據服務1.0我能夠加載和綁定各種列表(簡單),沒問題我不覺得我的環境事情,這就是爲什麼我覺得這是一個LINQ問題比技術更多。

我的問題是對於我有3個錶鏈接的場景,即實體(集合)。

例如,我在我的EDM中有這個:Employee - EmployeeProject - Project。

下面是從數據庫中的表設計:

Employee (table1) 
------------- 
EmployeeID (PK) 
FirstName 
other Attributes ... 

EmployeeProject (table2) 
------------- 
EmployeeProjectID (PK) 
EmployeeID (FK) 
ProjectID (FK) 
AssignedDate 
other Attributes ... 

Project (table3) 
------------- 
ProjectID (PK) 
Name 
other Attributes ... 

下面是從實體框架的EDM設計:

------------------------ 
Employee (entity1) 
------------------------ 
(Scalar Properties) 
------------------- 
EmployeeID (PK) 
FirstName 
other Attributes ... 
------------------- 
(Navigation Properties) 
------------------- 
EmployeeProjects 

------------------------ 
EmployeeProject (entity2) 
------------------------ 
(Scalar Properties) 
------------------- 
EmployeeProjectID (PK) 
AssignedDate 
other Attributes ... 
------------------- 
(Navigation Properties) 
------------------- 
Employee 
Project 

------------------------ 
Project (entity3) 
------------------------ 
(Scalar Properties) 
------------------- 
ProjectID (PK) 
Name 
other Attributes ... 
------------------- 
(Navigation Properties) 
------------------- 
EmployeeProjects 

到目前爲止,我只能夠做到:

var filteredList = Context.Employees 
    .Where(e => e.EmployeeProjects.Where(ep => ep.Project.Name == "ProjectX")) 

注意:我在John的帖子後更新了查詢的語法。如你所見,我只能查詢相關實體(EmployeeProjects)。我想要的只是能夠從Employee實體過濾到Project。

感謝您的任何意見。

回答

1

如果我理解你的問題正確,你正在尋找的東西是這樣的:

var filteredList = employees.Where(e => e.EmployeeProjects.Count(ep => ep.Project.Name == "Some project name") > 0) 
+0

約翰感謝您的快速回復。我試着輸入你的查詢,看起來WCF數據服務不支持URI中的Count。我可能在我的初始查詢語法中誤導了。請忽略計數,這只是我嘗試過濾列表。 所以我現在想是這樣的: VAR filteredList = Context.Employees 。凡(E => e.EmployeeProjects.Where(EP => ep.Project.Name == 「projectX創建」)) 你看看我想做什麼? – user118190 2010-04-21 17:10:15