[英]EF Query with conditional include that uses Joins
This is a follow up to another user's question . 这是对另一个用户的问题的跟进。 I have 5 tables 我有5张桌子
FK to CompanyDetail
公司联系FK to CompanyDetail
FK to CompanyContact
CompanyContactsSecurity FK to CompanyContact
FK to UserDetail
将UserGroupMembership FK to UserDetail
How do I return all companies and include the contacts in the same query? 如何返回所有公司并将联系人包含在同一查询中? I would like to include companies that contain zero contacts. 我想包括零接触的公司。
Companies have a 1 to many association to Contacts, however not every user is permitted to see every Contact. 公司与联系人的联系是一对多,但是并非每个用户都可以看到每个联系人。 My goal is to get a list of every Company regardless of the count of Contacts, but include contact data. 我的目标是获取每个公司的列表,而不考虑联系人的数量,但包括联系人数据。
Right now I have this working query: 现在我有这个工作查询:
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;
However this is inefficient when I need to get all contacts for all companies, since I use a For..Each loop and query each company individually and update my viewmodel. 但是,当我需要获取所有公司的所有联系人时,这效率很低,因为我使用For..Each循环并分别查询每个公司并更新我的视图模型。 Question: How do I shoehorn the permittedContacts variable above and insert that into this query: 问题:我该如何刺穿上面的allowedContacts变量并将其插入到此查询中:
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;
My attempt at doing this resulted in: 我尝试这样做的结果是:
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;
Perhaps something like this. 也许是这样的。
var q = from company in _entities.CompanyDetail
where
(from c in userGroupsQueryable
join p in contactsGroupsQueryable on c equals p.GroupID
where company.CompanyContacts.Any(cc => cc.pkCompanyContact == p.fkCompanyContact)
select p
).Any()
select new
{
Company = company,
Contacts = company.CompanyContacts
};
The following code queries all companies, and appends only the contacts the user is permitted to see. 以下代码查询所有公司,并仅附加允许用户查看的联系人。 I don't know how efficient this is, or if there is a way to make it faster, but it works. 我不知道这有多有效,或者是否有办法使其更快,但它确实有效。
var userGroupsQueryable = _entities.UserGroupMembership.Where(ug => ug.UserID == UserID)
.Select(a => a.GroupMembership);
var contactsGroupsQueryable = _entities.CompanyContactsSecurity;
var listOfCompanies =
from company in _entities.CompanyDetail
select new
{
Company = company,
Contacts = (from c in userGroupsQueryable
join p in contactsGroupsQueryable on c equals p.GroupID
where company.CompanyContacts.Any(cc => cc.CompanyID == p.CompanyID)
select p.CompanyContacts)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.