[英]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出于说明目的,我有一个简单的 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. RemoveDuplicates 方法将删除 Employee 对象的 Certifications 属性中的任何重复字符串。 Now consider if I have a list of Employee objects.现在考虑我是否有一个 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 对象列表中获取唯一认证列表?
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();
您想使用 SelectMany,它允许您选择子列表,但以扁平形式返回它们:
stringList = empList.SelectMany(emp => emp.Certifications).Distinct().ToList();
您可以尝试使用 SelectMany 扩展吗?
var employeeCertifications = (from e in employeeList select e.Certifications).SelectMany(x => x).Distinct();
If you want to have a set type for the variable, try Select()
instead:如果您想为变量设置一个类型,请尝试Select()
:
List<string> employeeCertifications =
employeeList.Select(e => e.Certifications).Distinct().ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.