使用List 作为属性从对象列表中获取唯一值

时间:2022-03-09 21:14:56

For illustrative purposes I've got a simple Employee class with several fields and a method to remove multiple occurrences in the Certifications property

为了便于说明,我有一个简单的Employee类,其中包含几个字段和一个删除Certifications属性中多次出现的方法

public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private List<string> certifications = new List<string>();
        public List<string> Certifications
        {
            get { return certifications; }
            set { certifications = value; }
        }

public List<string> RemoveDuplicates(List<string> s)
        {
            List<string> dupesRemoved = s.Distinct().ToList();
            foreach(string str in dupesRemoved)
                Console.WriteLine(str);
            return dupesRemoved;
        }

The RemoveDuplicates method will work remove any duplicate strings in the Certifications property of the Employee object. Now consider if I have a list of Employee objects.

RemoveDuplicates方法将删除Employee对象的Certifications属性中的任何重复字符串。现在考虑我是否有一个Employee对象列表。

 Employee e = new Employee();
           List<string> stringList = new List<string>();
           stringList.Add("first");
           stringList.Add("second");
           stringList.Add("third");
           stringList.Add("first");
           e.Certifications = stringList;
          // e.Certifications = e.RemoveDuplicates(e.Certifications); works fine

           Employee e2 = new Employee();
           e2.Certifications.Add("fourth");
           e2.Certifications.Add("fifth");
           e2.Certifications.Add("fifth");
           e2.Certifications.Add("sixth");

           List<Employee> empList = new List<Employee>();
           empList.Add(e);
           empList.Add(e2);

I could use

我可以用

foreach (Employee emp in empList)
           {
               emp.Certifications = emp.RemoveDuplicates(emp.Certifications);
           }

to get a list of ALL unique Certifications, from all employees in the List but I would like to do this in LINQ, something akin to

获取列表中所有员工的所有唯一认证列表,但我想在LINQ中执行此操作,类似于

stringList = empList.Select(emp => emp.Certifications.Distinct().ToList());

this gives me an error saying

这给了我一个错误的说法

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'. An explicit conversion exists (are you missing a cast?)  

How can I get a list of unique Certifications from a list of Employee objects?

如何从Employee对象列表中获取唯一认证列表?

3 个解决方案

#1


21  

If I understand, you want a list of all of the unique certifications among all of the employees. This would be a job for SelectMany:

如果我理解,您需要列出所有员工中的所有独特认证。这将是SelectMany的工作:

var uniqueCerts = empList.SelectMany(e => e.Certifications).Distinct().ToList();

#2


3  

You want to use SelectMany, which lets you select sublists, but returns them in a flattened form:

您想使用SelectMany,它允许您选择子列表,但以平面形式返回它们:

stringList = empList.SelectMany(emp => emp.Certifications).Distinct().ToList();

#3


0  

You may try using the SelectMany extension?

您可以尝试使用SelectMany扩展程序吗?

var employeeCertifications = (from e in employeeList select e.Certifications).SelectMany(x => x).Distinct();

#1


21  

If I understand, you want a list of all of the unique certifications among all of the employees. This would be a job for SelectMany:

如果我理解,您需要列出所有员工中的所有独特认证。这将是SelectMany的工作:

var uniqueCerts = empList.SelectMany(e => e.Certifications).Distinct().ToList();

#2


3  

You want to use SelectMany, which lets you select sublists, but returns them in a flattened form:

您想使用SelectMany,它允许您选择子列表,但以平面形式返回它们:

stringList = empList.SelectMany(emp => emp.Certifications).Distinct().ToList();

#3


0  

You may try using the SelectMany extension?

您可以尝试使用SelectMany扩展程序吗?

var employeeCertifications = (from e in employeeList select e.Certifications).SelectMany(x => x).Distinct();