我的表结构如下:
Person 1-M PesonAddress
Person 1-M PesonPhone
Person 1-M PesonEmail
Person 1-M Contract
Contract M-M Program
Contract M-1 Organization
在此查询结束时,我需要一个填充的对象图,其中每个人都有他们的:
- 个人地址
- 个人电话的
- PesonEmail的
- 个人电话的
- 合同的 - 这有其各自的
- 程式
现在我有以下查询,我认为它工作得很好,但它有几个问题:
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 的合同,而且每个合同的程序也同样如此。
我想要的只是那些至少有一个合同的组织 ID 为 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 对象):
- 我只能自己做映射,在这种情况下,有很多东西要映射
- 当我尝试将列表映射到属性(即奖学金 = currentScholarships)时,它说我不能因为
IEnumerable
试图被转换为EntityCollection
- 包含不起作用
因此,我如何让它发挥作用。请记住,我正在尝试将其作为编译查询来执行,所以我认为这意味着匿名类型已被淘汰。