我有一个有些非规范化的架构。请考虑以下表格:
Address
Contact
Company
ContactAddress (ContactId, AddressId)
Director (CompanyId, ContactAddressId)
如果联系人有多个地址,并且他们是公司的董事,他们将在Director表中有两行(每个地址一行 - 可能但不总是)。
我需要返回作为导演的联系人列表。每个联系人都有一个针对该公司的ContactAddresses集合。
问题是有100ks行(所以我并不热衷于从db加载所有内容),但是需要分页,即在不同的导向器上(而不是它们的地址)。网格中的每个Director都有每个地址一个子行。
e.g。
Bob Smith
Acme Company, 1 The Street, London
Acme Company, 3 The Terrace, Scarborough
Julie Hurts
Bobbies Bits, 5 Somewhere Land
Sarah Saysno
BikesRUs, 99 Nowhere land, Nowhere
挣扎着看看我如何用纯Linq to Entities做到这一点。
任何?
修改在域语言术语中 - 联系人可以拥有多个地址。联系人可以是其公司的多个地址的董事。公司可以在多个地址拥有多个董事
显示公司的所有联系人,这些联系人按联系人地址分组,他们是公司的董事,负责该地址。
最近我得到的是两个查询传递:
var directors = (
from companyDirector in ctx.CompanyDirectors
join contactAddress in ctx.ContactAddresses
on companyDirector.ContactAddress equals contactAddress
join contact in ctx.Contacts
on contactAddress.Contact equals contact
where contact.DisplayName.Contains(searchText)
&& companyDirector.TypeId == CompanyDirector.DirectorTypeId
orderby contact.DisplayName
group companyDirector by new {companyDirector.Company, companyDirector.ContactAddress.Contact}
into companyContacts
select companyContacts
).Page(pageNumber, pageSize).ToList();
var query = (
from director in directors
select new CompanyDirectorLocations
{
CompanyId = director.Key.Company.Id,
ContactDisplayName = director.Key.Contact.DisplayName,
Locations = (
from companyDirector in ctx.CompanyDirectors
where companyDirector.Company == director.Key.Company
&& companyDirector.ContactAddress.Contact == director.Key.Contact
&& companyDirector.TypeId == CompanyDirector.DirectorTypeId
select companyDirector.ContactAddress.Address.City
).ToList()
}
);
答案 0 :(得分:0)
这“正常运作”。如果你这样做:
var q = (from c in Context.Contacts
where c.Director != null
select new ContactPresentation
{
Name = c.Name,
Addresses = from a in c.Addresses
select new AddressPresentation
{
Company = a.Company,
// etc.
}).Take(3);
...然后您获得前3个联系人,而不是前3个地址。
你试过吗?