我有两个实体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和Navigation Properties实现此目的,我将非常感谢您的帮助。

非常感谢

最佳答案

假设您在候选人和候选人位置之间存在一对多关联

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导航属性复合体,其中包含ID(从...中选择ID),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14307261/

10-10 22:26