【问题标题】:Getting unique values from a list of objects with a List<string> as a property从具有 List<string> 作为属性的对象列表中获取唯一值
【发布时间】:2013-06-18 15:05:46
【问题描述】:

出于说明的目的,我有一个简单的 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;
        }

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

我可以使用

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

从列表中的所有员工那里获取所有独特认证的列表,但我想在 LINQ 中执行此操作,类似于

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 对象列表中获取唯一认证列表?

【问题讨论】:

  • 如果 EmployeeId 是唯一标识符,那么我建议您使用 EmployeeId 覆盖 GetHashCode 和 Equals。还要将 List 认证设为 HashSet(不是 List),以不允许在 Employee 中重复。
  • 这仅用于说明目的,但感谢您指出这一点,我会将其归档到工具集中。

标签: c# asp.net visual-studio-2010 linq


【解决方案1】:

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

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

【讨论】:

  • 我知道我必须接近,而且它是一个班轮 :)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      如果您想为变量设置一个类型,请尝试使用Select()

      List<string> employeeCertifications =
          employeeList.Select(e => e.Certifications).Distinct().ToList();
      

      【讨论】:

        猜你喜欢
        • 2012-04-18
        • 2013-06-25
        • 1970-01-01
        • 2018-04-13
        • 1970-01-01
        • 1970-01-01
        • 2022-06-15
        • 2017-01-07
        • 2021-05-02
        相关资源
        最近更新 更多