0

There are Candidate and Job entities:

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();
        }
    }
}

For each job, I want to get the candidates with most matching skills. This LINQ query returns an error. is there a better LINQ query to get the results?

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())));
}
3
  • Use Join instead of where and any method. Commented Oct 9, 2018 at 18:55
  • @Sham how does that help in this context? Commented Oct 9, 2018 at 18:58
  • FYI your list properties can be made simpler just by doing: public List<string> skillsList => Skills.Split(',').Select(s => s.Trim()).ToList(); Commented Oct 9, 2018 at 19:04

1 Answer 1

2

For each job project (.Select) a new object containing the job and the candidate with the top number of matching skills (OrderDescendingBy the number of intersections):

var result = jobs.Select(j => new {
    Job = j,
    Candidate = candidates.OrderByDescending(c => c.skillTagsList.Intersect(j.skillsList).Count())
                          .First()
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes your solution solved my problem, thanks for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.