5

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

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

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

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

规则

在 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 和导航属性来实现这一点,我将不胜感激。

非常感谢

4

1 回答 1

6

假设您在 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))
);
于 2013-01-13T22:06:23.573 回答