简体   繁体   English

从 3 个不同的列表中获取匹配对象的列表

[英]Get list of matching objects from 3 different lists

I'm exercising on one task.我正在执行一项任务。 Basically i got 3 different lists in C# code side.基本上我在 C# 代码方面得到了 3 个不同的列表。 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 }.假设我们有 3 名员工,并且所有 3 名员工都有共同的 JAVA 技能,例如 { id = x , name = JAVA }。

so all 3 employees have skills similar with id and name needs to be fetched out.所以所有 3 名员工都具有与 id 和 name 相似的技能,需要取出。

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.但这会让 IEnumerable 出错并且我无法形成 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 SelectMany to get to the child lists.关键是使用SelectMany来获取子列表。

public class Employee
{
    public List<Skills> Skills { get; set; }
}

public class Skills
{
    public int Id { get; set; }
    public string Name { get; set; }
}

[Test]
public void GetSomeSkills()
{
    var employees = new List<Employee>
    {
        new Employee { Skills = new List<Skills> { new Skills { Id = 1, Name = "Java" }, new Skills { Id = 2, Name = "C#" } } },
        new Employee { Skills = new List<Skills> { new Skills { Id = 1, Name = "Java" }, new Skills { Id = 3, Name = "Cooking" } } },
        new Employee { Skills = new List<Skills> { new Skills { Id = 1, Name = "Java" } } },
        //new Employee { Skills = new List<Skills> { new Skills { Id = 4, Name = "C++" } } }
    };

    var allSkills = employees.SelectMany(x => x.Skills).ToList();
    
    Console.WriteLine(string.Join(", ", allSkills.Select(x => x.Name)));
    // Output: Java, C#, Java, Cooking, Java

    var commonSkills = employees.SelectMany(e =>
        e.Skills.Where(s => employees.All(e2 => e2.Skills.Select(x => x.Id).Contains(s.Id)))).ToList();

    Console.WriteLine(string.Join(", ", commonSkills.Select(x => x.Name)));
    // Output: Java, Java, Java
}

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.以下仅输出每个人都拥有的技能,如果您取消注释最后一个员工,您将获得 null 作为结果。

[Test]
public void GetSomeSkills()
{
    var employees = new List<Employee>
    {
        new Employee { Skills = new List<Skills> { new Skills { Id = 1, Name = "Java" }, new Skills { Id = 2, Name = "C#" } } },
        new Employee { Skills = new List<Skills> { new Skills { Id = 1, Name = "Java" }, new Skills { Id = 2, Name = "C#" } } },
        // new Employee { Skills = new List<Skills> { new Skills { Id = 1, Name = "Java" } } },
    };

    bool HasSameSkills(Employee first, Employee second)
    {
        var firstIds = first.Skills.Select(x => x.Id).OrderBy(x => x).ToList();
        var secondIds = second.Skills.Select(x => x.Id).OrderBy(x => x).ToList();
        return firstIds.SequenceEqual(secondIds);
    }

    var commonSkills = employees.FirstOrDefault(x => employees.All(y => HasSameSkills(x, y)))?.Skills;

    Console.WriteLine(string.Join(", ", (commonSkills ?? new List<Skills>()).Select(x => x.Name)));
    // Output: Java, C#
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM