验证复杂数据时出现问题(同时显示最终值)

时间:2019-06-14 11:57:44

标签: c#-4.0

我在同一记录中有两次列出的对象列表。但是,当我应用与众不同的方法时,它不会删除,因为它区分大小写。

我尝试了三种方式:

  1. 列出转换为匿名类型的数据
  2. 通过重写equals和哈希码在类中实现了I Equal CompareEquals
  3. 实现I compareequals接口的单独类。

   static void Main(string[] args)
   {
        List<Employee> lstEmployee = new List<Employee>() 
        { 
              new Employee 
            { 
               sID = 100, 
               Name = "ASHOK" 
            }, 
            new Employee 
            {
               sID = 101, 
               Name = "SHAKTHI" 
            }, 
            new Employee 
            { 
               sID = 102, 
               Name = "RAVI" 
            }, 
            new Employee
            {
               sID=100,
               Name="ashok"
            } 
        };
        var lstEmployeeDetails = lstEmployee.Distinct(new EmployeeComparer());
   }  
    public class Employee
    {
       public int sID { get; set; }
       public string Name { get; set; } 
       public override bool Equals(object obj)
       {
         return this.sID == ((Employee)obj).sID && this.Name == 
         ((Employee)obj).Name;
       }
       public override int GetHashCode()
       {
         return this.sID.GetHashCode() ^ this.Name.GetHashCode();
       }
    }    


    public class EmployeeComparer : IEqualityComparer<Employee>
    { 
       public bool Equals(Employee x, Employee y)
       { 
          return x.sID == y.sID && x.Name == y.Name;
        }
        public int GetHashCode(Employee obj)
        { 
          return obj.sID.GetHashCode()^ obj.Name.GetHashCode(); 
        }
    }    

在列表中重复数据sid = 101两次(重复),小写。我只需要获取“ ASHOK”和id = 101

0 个答案:

没有答案