Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting unique values from a list of objects with a List<string> as a property

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

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

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

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?

like image 891
wootscootinboogie Avatar asked Jun 18 '13 15:06

wootscootinboogie


People also ask

How do you find the unique list of values?

Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.

How do I find unique elements in an ArrayList?

Let's see how to get unique values from ArrayList. Convert ArrayList to HashSet to insert duplicate values in ArrayList but on the other hand, HashSet is not allowing to insert any duplicate value. While converting ArrayList to HashSet all the duplicate values are removed and as a result, unique values are obtained.


2 Answers

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();
like image 166
Joe Enos Avatar answered Oct 17 '22 00:10

Joe Enos


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

stringList = empList.SelectMany(emp => emp.Certifications).Distinct().ToList();
like image 22
RoadieRich Avatar answered Oct 17 '22 00:10

RoadieRich