.net - LINQ 到 SQL : How to check if any item in one entity collection exists in another entity collection?

标签 .net asp.net-mvc linq entity-framework linq-to-sql

我正在使用 MVC2 和 Entity Framework 。

我有 2 个实体集合,我需要比较它们并检查它们是否有任何共同点。例如,假设我有 EntityCollection<Candidate>EntityCollection<Job> 。我正在尝试返回所有具有该职位首选技能中列出的技能的候选人。这是正确的吗:

public IQueryable<Candidate> GetMatchingCandidates(Job job)
{                
     return from candidate in _db.Candidates
     where (candidate.CandidateSkills.Where(c => job.JobPreferredSkills.Any(j => j.SkillId== c.SkillId)).Count() > 0) 
     select candidate;                                
}

同样,我也希望获得具备首选技能中列出的所有技能的候选人。

最佳答案

在第一种情况下我会使用 Any():

    public IQueryable<Candidate> GetMatchingCandidates(Job job)
    {
        return from candidate in _db.Candidates
               where (candidate.CandidateSkills.Any(c => job.JobPreferredSkills.Any(j => j.SkillId == c.SkillId)))
               select candidate;
    }

然后对第二种情况使用All()(所有技能都必须在首选技能中)

    public IQueryable<Candidate> GetMatchingCandidates(Job job)
    {
        return from candidate in _db.Candidates
               where (candidate.CandidateSkills.All(c => job.JobPreferredSkills.Any(j => j.SkillId == c.SkillId)))
               select candidate;
    }

关于.net - LINQ 到 SQL : How to check if any item in one entity collection exists in another entity collection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6564194/

相关文章:

c# - 按单词列表在 LINQ 中的第一个字母分组

.net - 如何在winform列表框项目上添加工具提示

c# - 通过自连接更新 List<T>?

c# - EF 7 : INSERT statement conflicted with the FOREIGN KEY SAME TABLE

asp.net-mvc - 带有 vNext : Do we still need the Global. asax 的 MVC 6?

c# - Linq 选择昨天的日期

linq - 如果我从 XDocument 选择节点,顺序是否始终保留?

.net - Visual basic 2010 检查表单加载时文件是否存在

C#:让我的程序调用同一台机器上正在运行的进程的方法

asp.net-mvc - 解析 Controller 和操作上的 FilterAttributes