2010-04-21 125 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