gpt4 book ai didi

c# - Linq Navigation Properties complex where ID in (select id from...)

转载 作者:太空狗 更新时间:2023-10-29 23:08:57 25 4
gpt4 key购买 nike

我有两个实体 Candidate 和 CandidateLocation,其中 Candidate 可以有多个 CandidateLocation 条目。

CandidateLocation 包含 CandidateId、ISO 国家代码(例如美国、英国)和类型列(1 = 允许,2 = 受限)。

规则规定,如果候选人在 CandidateLocation 表中没有任何“允许”条目,他们可以在任何地方工作。如果他们有明确的“允许”位置,则他们只能在明确允许的位置工作。他们不能在明确限制的地点工作。

要尝试演示这一点,请参见下图(候选人可以有多个位置,我将其保留为一个以简化说明)

Rules

在 SQL 中,实现此目的的一种方法是以下查询

SELECT  *
FROM Candidate
WHERE Candidate.IsArchived = 0
AND
-- Do not inlude restricted locations (RestrictionStatus = 2)
Candidate.CandidateId NOT IN (SELECT CandidateId FROM CandidateLocation WHERE IsArchived = 0 AND CountryISOCode = @Location AND RestrictionStatus = 2)
AND
(
-- Include Explicit Permitted Locations
Candidate.CandidateId IN (SELECT CandidateId FROM CandidateLocation WHERE IsArchived = 0 AND CountryISOCode = @Location AND RestrictionStatus = 1)
OR
-- Include Candidates with no Explicit Permitted Locations
Candidate.CandidateId NOT IN (SELECT CandidateId FROM CandidateLocation WHERE IsArchived = 0 AND RestrictionStatus = 1)
)

如果有人知道如何使用 linq 和导航属性实现此目的,我将不胜感激。

非常感谢

最佳答案

假设您在 Candidates 和 CandidateLocations 之间建立了一对多关联

Context.Candidates.Where(c => c.IsArchived == 0 &&
!c.CandidateLocations.Any(
l => l.CountryISOCode == location && l.RestrictionStatus == 2) &&
(c.CandidateLocations.Any(
l => l.CountryISOCode == location && l.RestrictionStatus == 1) ||
!c.CandidateLocations.Any(
l => l.RestrictionStatus == 1))
);

关于c# - Linq Navigation Properties complex where ID in (select id from...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14307261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com