我正在尝试连接两个表。
TSQL 将是:
SELECT *
FROM User u INNER JOIN Hierarchy h ON u.OrganisationId = h.OrganisationId
OR u.OrganisationId = h.OwnerOrganisationId
我已经搜索过,没有人能流利地回答这个问题。我能想到的最接近的是:
var join1 = context.User.Join(context.Hierarchy, u => u.OrganisationId, h => h.OrganisationId, uh => new {u, h});
var join2 = context.User.Join(context.Hierarchy, u => u.OrganisationId, h => h.OwnerOrganisationId, uh => new {u, h});
var desiredResult = join1.Union(join2);
不过,这似乎效率很低。
流利的语法
var orJoin = context.User.SelectMany(
u => context.Hierarchy.Where(h => u.OrganisationId == h.OrganisationId || u.OrganisationId == h.OwnerOrganisationId),
(u, h) => new { u, h }
);
查询语法
var orJoin = from u in context.User
from h in context.Hierarchy
where u.OrganisationId == h.OrganisationId || u.OrganisationId == h.OwnerOrganisationId
select new { u, h };