这是跟进to another user's question。我有5张桌子带有条件的EF查询使用连接的查询
- CompanyDetail
- CompanyContacts
FK to CompanyDetail
- CompanyContactsSecurity
FK to CompanyContact
- UserDetail
- UserGroupMembership
FK to UserDetail
如何退还所有的公司,其中包括在同一查询联系人?我想包括包含零联系人的公司。
公司与联系人有1对多关联,但不是每个用户都被允许查看每个联系人。我的目标是获得每个公司的名单,而不管联系人的数量,但包括联系人数据。
现在,我有这方面的工作查询:
var userGroupsQueryable = _entities.UserGroupMembership
.Where(ug => ug.UserID == UserID)
.Select(a => a.GroupMembership);
var contactsGroupsQueryable = _entities.CompanyContactsSecurity;//.Where(c => c.CompanyID == companyID);
/// OLD Query that shows permitted contacts
/// ... I want to "use this query inside "listOfCompany"
///
//var permittedContacts= from c in userGroupsQueryable
//join p in contactsGroupsQueryable on c equals p.GroupID
//select p;
然而,这是低效的,当我需要得到所有公司的所有联系人,因为我用的是For..Each循环和单独和更新查询每家公司我的视角模型。 问:如何鞋拔的permittedContacts变量上,并插入到该查询:
var listOfCompany = from company in _entities.CompanyDetail.Include("CompanyContacts").Include("CompanyContactsSecurity")
where company.CompanyContacts.Any(
// Insert Query here....
// b => b.CompanyContactsSecurity.Join(/*inner*/,/*OuterKey*/,/*innerKey*/,/*ResultSelector*/)
)
select company;
我在做这样的尝试导致:
var listOfCompany = from company in _entities.CompanyDetail.Include("CompanyContacts").Include("CompanyContactsSecurity")
where company.CompanyContacts.Any(
// This is concept only... doesn't work...
from grps in userGroupsQueryable
join p in company.CompanyContactsSecurity on grps equals p.GroupID
select p
)
select company;
你说你的目标是得到每个公司的名单,不管联系人的数量。但它不可能那么简单,或者你只是得到公司名单而不管联系人。你能提供一些关于你如何筛选公司的更多细节(用文字)吗? –
@安妮。公司根本没有被过滤。我想禁用延迟加载并允许单个SQL查询给我列出所有公司和相关联系人的结果,但限制是不是每个用户都可以看到每个联系人。 – LamonteCristo