Get distinct list values

后端 未结 8 898
余生分开走
余生分开走 2020-12-17 17:58

i have a C# application in which i\'d like to get from a List of Project objects , another List which contains distinct objects.

i trie

8条回答
  •  生来不讨喜
    2020-12-17 18:39

    How do you define identical? You should override Equals in Project with this definition (if you override Equals also override GetHashCode). For example:

    public class Project
    {
        public int ProjectID { get; set; }
    
        public override bool Equals(object obj)
        {
            var p2 = obj as Project;
            if (p2 == null) return false;
            return this.ProjectID == m2.ProjectID;
        }
    
        public override int GetHashCode()
        {
            return ProjectID;
        }
    }
    

    Otherwise you are just checking reference equality.

提交回复
热议问题