public class TestDuplicateDefine : IEqualityComparer<student> { public bool Equals(student x, student y) { return x.name == y.name; } public int GetHashCode(student obj) { return obj.ToString().GetHashCode(); } } public class student { public string name { get; set; } public int id { get; set; } }
private void button1_Click(object sender, EventArgs e) { List<student> list = new List<student>(); list.Add(new student { name = "111", id = 1 }); list.Add(new student { name = "222", id = 2 }); list.Add(new student { name = "111", id = 3 }); list.Add(new student { name = "333", id = 4 }); List<student> aa = list.Distinct(new TestDuplicateDefine()).ToList(); }