C# IEqualityComparer 去重
1、去除list里某重复字段值的数据(相当于group by)
public class CorrController { //方法 public void DoGet() { List<test11> list_test = new List<test11>(); list_test.Add(new test11() { m = 1, v = "one" }); list_test.Add(new test11() { m = 2, v = "two" }); list_test.Add(new test11() { m = 3, v = "three" }); list_test.Add(new test11() { m = 4, v = "fornt" }); list_test.Add(new test11() { m = 4, v = "fornt" }); list_test.Add(new test11() { m = 3, v = "fornt" }); var ss = list_test.Distinct(new Comparint());//这里调用 } } //model public class test11 { public int m { get; set; } public string v { get; set; } } // public class Comparint : IEqualityComparer<test11> { public bool Equals(test11 x, test11 y) { if (x == null && y == null) return false; return x.m == y.m; } public int GetHashCode(test11 obj) { return obj.ToString().GetHashCode(); } }
执行Distinct前有6条数据:
执行Distinct后有4条数据: