1、去除list里某重复字段值的数据(相当于group by)

    public class CorrController
{
//方法
public void DoGet()
{
List<test11> list_test = new List<test11>();
list_test.Add(new test11()
{
m = ,
v = "one"
});
list_test.Add(new test11()
{
m = ,
v = "two"
});
list_test.Add(new test11()
{
m = ,
v = "three"
});
list_test.Add(new test11()
{
m = ,
v = "fornt"
});
list_test.Add(new test11()
{
m = ,
v = "fornt"
});
list_test.Add(new test11()
{
m = ,
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条数据:

IEqualityComparer 去重-LMLPHP

执行Distinct后有4条数据:

IEqualityComparer 去重-LMLPHP

05-02 11:51