My table structure is as follows:
Person 1-M PesonAddress Person 1-M PesonPhone Person 1-M PesonEmail Person 1-M Contract Contract MM Program Contract M-1 Organization
At the end of this query, I need a graph of the completed object, where each person has:
- Pesonaddress in
- PesonPhone's
- PesonEmail's
- PesonPhone's
- Contract - and it has
Now I had the following query, and I thought it worked fine, but it has a few problems:
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;
The problem is that it filters a person according to criteria, and not according to contracts or contract programs. He returns all Contracts that each person has not only those that have Organization x, and the same applies to each of these Contract Programs, respectively.
What I want is only people who have at least one contract with OrgId from x and where this contract has a program with identifier y ... and for an object graph that returns to have only contracts this is a correspondence and programs under this contract that are consistent.
I understand why it does not work, but I do not know how to change it, so it works ...
This is my attempt:
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, ... };
But this has several problems (where the Person type is an EF object):
- It remains for me to do the mapping myself, which in this case is quite a lot to display
- When ever I try to map a list to a property (i.e. scholarship = currentScholarships), it says that I cannot, because
IEnumerable trying to be different from EntityCollection - Enable not working
Therefore, how do I get this to work. Remembering that I am trying to do this as a compiled request, so I think this means that anonymous types are not available.