使用List< string>从对象列表中获取唯一值.作为财产 [英] Getting unique values from a list of objects with a List<string> as a property

查看:200
本文介绍了使用List< string>从对象列表中获取唯一值.作为财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于说明目的,我有一个简单的Employee类,其中包含多个字段,并且提供了一种方法来删除Certifications属性中的多次出现

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

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

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

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.

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

我可以使用

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

从列表中的所有员工那里获取所有唯一证书的列表,但是我想在LINQ中做到这一点,类似于

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

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

这给我一个错误提示

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?)  

如何从Employee对象列表中获得唯一证书列表?

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

推荐答案

据我了解,您需要列出所有员工中所有唯一认证的列表. SelectMany :

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:

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

这篇关于使用List&lt; string&gt;从对象列表中获取唯一值.作为财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆