比较C#中的两个字符串列表

时间:2018-10-09 18:53:20

标签: c# list linq compare

有候选人和工作实体:

public class Candidate
{
    public int CandidateId { get; set; }
    public string Name { get; set; }
    public string SkillTags { get; set; }

    public List<string> skillTagsList
    {
        get
        {               
            return Array.ConvertAll(SkillTags.Split(','), p => p.Trim()).ToList();
        }
    }
}

public class Job
{
    public int JobId { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Skills { get; set; }

    public List<string> skillsList
    {
        get
        {
            return Array.ConvertAll(Skills.Split(','), p => p.Trim()).ToList();
        }
    }
}

对于每一项工作,我都希望获得具有最匹配技能的候选人。 此LINQ查询返回错误。有没有更好的LINQ查询来获取结果?

List<Candidate> candidates = repository.GetCandidates().Result;
List<Job> jobs = repository.GetJobs().Result;
List<Candidate> JobCandidates = null;
jobs.ForEach(j =>
{
    JobCandidates = candidates.Where(c => c.skillTagsList.Any(st => j.skillsList.Contains(st.ToLower())));
}

1 个答案:

答案 0 :(得分:2)

对于每个工作项目(.Select),一个新对象包含工作和匹配技能最高的候选人(OrderDescendingBy的相交数):

var result = jobs.Select(j => new {
    Job = j,
    Candidate = candidates.OrderByDescending(c => c.skillTagsList.Intersect(j.skillsList).Count())
                          .First()
});