平面层次结构上的 Linq to Entities 分页
我有一个有点非规范化的模式。请考虑以下表格:
Address
Contact
Company
ContactAddress (ContactId, AddressId)
Director (CompanyId, ContactAddressId)
如果联系人有多个地址,并且他们是一家公司的董事,则他们在董事表中将有两行(每个地址对应一个 - 可能但并非总是如此)。
我需要返回董事联系人列表。每个联系人都有该公司的联系人地址集合。
问题是有 100ks 行(所以我不热衷于从数据库加载所有内容),但需要分页,即在不同的控制器上(而不是它们的地址)。网格中的每个主管都会有一个子行,对应每个地址。
例如,
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()
}
);
I have a schema that is somewhat denormalised. consider the following tables:
Address
Contact
Company
ContactAddress (ContactId, AddressId)
Director (CompanyId, ContactAddressId)
If a Contact has more than one address, and they are a Director of a company, they will have two rows in the Director table (one for each address - potentially but not always).
I need to return a list of Contacts that are Directors. Each Contact has a collection of ContactAddresses for this Company.
The problem is there's 100ks rows (so I'm not keen on loading everything from the db), but paging is required i.e. on the distinct directors (not their addresses). Each Director in the grid will have sub-rows one for each address.
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
Struggling to see how I could do that with pure Linq to Entities.
Anyone?
Edit In domain language terms - a contact can have more than one address. a contact can be a director of a company at more than one of his addresses. a company can have multiple directors at multiple addresses
Show all contacts at a company grouped by the contacts address where they are a director at the company for that address.
Closest Ive got is a two query pass:
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()
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这“行得通”。如果您这样做:
...那么您将获得前 3 个联系人,而不是前 3 个地址。
您尝试过吗?
This "just works." If you do:
...then you get the top 3 contacts, not the top 3 addresses.
Have you tried it?