【发布时间】:2010-05-06 05:30:13
【问题描述】:
我的表结构如下:
Person 1-M PesonAddress
Person 1-M PesonPhone
Person 1-M PesonEmail
Person 1-M Contract
Contract M-M Program
Contract M-1 Organization
在这个查询的最后,我需要一个填充的对象图,每个人都有他们的:
- PesonAddress 的
- PesonPhone 的
- PesonEmail 的
- PesonPhone 的
- 合同的 - 这有其各自的
- 节目的
现在我有以下查询,我认为它工作得很好,但它有几个问题:
from people in ctx.People.Include("PersonAddress")
.Include("PersonLandline")
.Include("PersonMobile")
.Include("PersonEmail")
.Include("Contract")
.Include("Contract.Program")
where people.Contract.Any(
contract => (param.OrganizationId == contract.OrganizationId)
&& contract.Program.Any(
contractProgram => (param.ProgramId == contractProgram.ProgramId)))
select people;
问题在于它将人员过滤到标准而不是合同或合同的程序。它带回了所有合同,每个人不仅拥有 OrganizationId 为 x 的合同,而且每个合同的程序也同样如此。
我想要的只是那些至少有一个合同的人,合同的 OrgId 为 x,并且该合同有一个 Id 为 y 的程序......并且返回的对象图只有合同匹配和该合同中匹配的程序。
我有点明白为什么它不起作用,但我不知道如何更改它以使其正常工作......
这是我迄今为止的尝试:
from people in ctx.People.Include("PersonAddress")
.Include("PersonLandline")
.Include("PersonMobile")
.Include("PersonEmail")
.Include("Contract")
.Include("Contract.Program")
let currentContracts = from contract in people.Contract
where (param.OrganizationId == contract.OrganizationId)
select contract
let currentContractPrograms = from contractProgram in currentContracts
let temp = from x in contractProgram.Program
where (param.ProgramId == contractProgram.ProgramId)
select x
where temp.Any()
select temp
where currentContracts.Any() && currentContractPrograms.Any()
select new Person { PersonId = people.PersonId, FirstName = people.FirstName, ..., ....,
MiddleName = people.MiddleName, Surname = people.Surname, ..., ....,
Gender = people.Gender, DateOfBirth = people.DateOfBirth, ..., ....,
Contract = currentContracts, ... }; //This doesn't work
但这有几个问题(其中 Person 类型是 EF 对象):
- 我只能自己做映射,在这种情况下,要映射的东西很多
- 当我尝试将列表映射到属性(即 Scholarship = currentScholarships)时,它说我不能,因为
IEnumerable正试图被转换为EntityCollection - 包含不起作用
因此,我该如何让它发挥作用。请记住,我正在尝试将其作为编译查询来执行,因此我认为这意味着匿名类型已被淘汰。
【问题讨论】:
-
您确定 Contract-Program 是 M-M 吗?那么有一个交叉引用表吗?
-
是的...在数据库中,在 Contract 和 Program 之间有一个表,它有助于 M-M,但 Entity 框架已经抽象出那个“实体”...
标签: .net linq subquery entity-framework-4