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?
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.
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.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With