您可以使用where
代替join
:
from a in Proposals
join b in Proposal_Minions on a.ID equals b.Proposal_ID
from c in Users
where a.PI_User_ID == c.ID || a.Creator_User_ID == c.ID || b.User_ID == c.ID
select new { a, b, c }
但是,这相当于INNER JOIN
. 您可以通过以下方式获得外连接效果
from a in Proposals
join b in Proposal_Minions on a.ID equals b.Proposal_ID into j
from pm in j.DefaultIfEmpty()
select new { a,
pm,
c = (from c in Users
where a.PI_User_ID == c.ID
|| a.Creator_User_ID == c.ID
|| pm.User_ID == c.ID
select c).FirstOrDefault()
}
因为你想选择Proposal
s 只有你可以做
from a in Proposals
join b in Proposal_Minions on a.ID equals b.Proposal_ID into j
from pm in j.DefaultIfEmpty()
where Users.Any(u => a.PI_User_ID == u.ID
|| a.Creator_User_ID == u.ID
|| pm.User_ID == u.ID)
select a
但这不再是外部连接Users
。另一方面,两者的外部连接Users
会Proposal_Minions
破坏它们的过滤器目的,因为Proposals
无论如何都会被选中。对于外部连接,这仍然适用Proposal_Minions
。