I'm exercising on one task. Basically i got 3 different lists in C# code side. Class employee is containing the skills list. And also have list of employees as well.
for example :
class Employee
{
List<Skills> Skills;
}
class Skills
{
int Id;
int Name;
}
class Where_Im_Working_Currently
{
List<Employee> employees ;
}
What I'm trying to achieve is to get list of common skills from every individual. suppose we have 3 employees and all 3 employees have JAVA skill in common such that { id = x , name = JAVA }.
so all 3 employees have skills similar with id and name needs to be fetched out.
NOTE : I'm trying to get all matching skills and not just subset of skills
for e.g.
Case 1: Perfect match. (Get List having a, b, c)
list 1 => a, b, c
list 2 => a, b, c
list 3 => a, b, c
Case 1: No match. (Get Null list)
list 1 => a, b, c
list 2 => a, b,
list 3 => b, c
following is the query i have come up with :
var skills= employees.Select(x => x.Skills.Select(p => p.Id== x.Skill[0].Id && p.Name == x.Skill[0].Name));
but this will give IEnumerable that's where its getting wrong and im unable to form LINQ.
Any pointers or help are welcomed.
This might not be the most optimized way of doing this, but here's a solution that outputs the skills that are common for all employees.
The key is to use
SelectManyto get to the child lists.If you uncomment the last Employee you would have a zero result, as there would no longer be a skill that is common for all employees.
Also you probably want to get distinct result, but it sounds like you already know how to do that.
Edit after original question was modified:
The below outputs only the skills that everyone has, if you uncomment the last Employee you would have null as result.