c#对象集合去重_C# 实现Distinct将对象按条件去重

时间:2024-10-08 07:08:51

平时,我们将c#中的Distinct大多用于对数组去重,一般数组为基础的数据类型,例如 int,string.也可以用于对象去重,我们看看C#对Distinct方法的定义:

有重载,第一个参数都加了this,是拓展方法,有关拓展方法,请百度了解。

下面我们来研究下Distinct的对象去重,假设我们现在有一个People类:

public class People

{

public int ID { get; set; }

///

/// 姓名

///

public string Name { get; set; }

///

/// 所属省份

///

public string Province { get; set; }

///

/// 年龄

///

public int Age { get; set; }

public override string ToString()

{

return ("ID:{0} Name:{1} Province:{2} Age:{3}", ID, Name, Province, Age);

}

}

我们声明一个ListPeole对象集合:

People p = new People() { ID = 100, Name = "liu", Province = "广东", Age = 100 };

People p1 = p;

People p2 = p1;

IEnumerable ListPeople = new List()

{

p,

p1,

p2,

new People(){ID=0,Name="li",Province="湖北",Age=20},

new People(){ID=0,Name="li",Province="湖北",Age=20},

new People(){ID=1,Name="li",Province="湖北",Age=20},

new People(){ID=1,Name="li",Province="湖南",Age=20},

new People(){ID=2,Name="li",Province="湖北",Age=20},

new People(){ID=3,Name="li",Province="湖南",Age=21},

new People(){ID=4,Name="li",Province="湖北",Age=22},

};

我们来对ListPeople使用Distinct方法,不带任何参数,运行结果如下:

可以看到,Distinct方法没有带参数的话,将对象集合中p,p1,p2进行去重,而对于对象的成员值是一样的不同对象没有去重,说明Distinct方法不加参数的话,去重的规则是比较对象集合中对象的引用是否相同,如果相同,则去重,否则不去重。

现在我们有个需求,对于ID相同的People,我们算做同一个人,要输出集合中不重复的人(对于ID相同的随便输出一个即可),这时,我们用到了Distinct的第二个方法,方法要求传入的参数是IEqualityComparer类型,继承一个泛型接口,我们加入如下代码:

public class PeopleCompareByID : IEqualityComparer

{

public bool Equals(People x, People y)

{

if (x == null || y == null)

return false;

if ( == )

return true;

else

return false;

}

public int GetHashCode(People obj)

{

if (obj == null)

return 0;

else

return ();

}

}

继承IEqualityComparer接口必须实现Equals和GetHashCode方法。

我们比较的时候,传入一个PeopleCompareByID 的实体即可:

(new PeopleCompareByID()).ToList().ForEach(x => Console.WriteLine(x));

运行结果如下:

达到了我们以ID去重的效果。

现在需求又变,ID和省份相同的算同一个人,要输出人的信息(相同的随便输出一个即可),这个时候,我们看到ID=0和Province="湖北"的存在重复,要将其去重,我们再来一个类,还是继承自IEqualityComparer:

public class PeopleCompareByIDAndProvince : IEqualityComparer

{

public bool Equals(People x, People y)

{

if (x == null || y == null)

return false;

if ( == &&==)

return true;

else

return false;

}

public int GetHashCode(People obj)

{

if (obj == null)

return 0;

else

return ()^();

}

}

同样,使用Distinct方法的时候,传入PeopleCompareByIDAndProvince 的实例:

(new PeopleCompareByIDAndProvince()).ToList().ForEach(x => (x));

运行后的结果如下:

新增:1.类的某个属性是list数组,按照这个list的引用是否相同来判断是否是同一个对象

我们再来修改一下上面的代码如下:

class Program

{

static void Main(string[] args)

{

List list = new List() { 1, 2, 11, 222, 3, 4 };

List list1 = new List() { 1, 2, 11, 222, 3, 4 };

People p1 = new People() { Name = "Tony1", ID = 1, Age = 18, Members = list };

People p2 = new People() { Name = "Tony2", ID = 2, Age = 19, Members = list };

People p3 = new People() { Name = "Tony3", ID = 3, Age = 20, Members = list1 };

People p4 = new People() { Name = "Tony4", ID = 4, Age = 21, Members = new List() };

List personList = new List() { p1, p2,p2,p3,p4 };

(new PeopleComparedByList()).ToList().ForEach(x => (x));

();

}

}

public class People

{

public int ID { get; set; }

///

/// 姓名

///

public string Name { get; set; }

///

/// 所属省份

///

public string Province { get; set; }

///

/// 年龄

///

public int Age { get; set; }

private List members = new List();

public List Members

{

get { return members; }

set { members = value; }

}

public override string ToString()

{

return ("ID:{0} Name:{1} Province:{2} Age:{3},Members:{4}", ID, Name, Province, Age,("-",()));

}

}

public class PeopleComparedByList : IEqualityComparer

{

public bool Equals(People x, People y)

{

if ( == )

return true;

else

return false;

}

public int GetHashCode(People obj)

{

return ();

}

}

运行的结果如下:

从结果可以看到,Tony1,Tony2的Members属性是一个同一个引用的list,所以去重复的时候把Tony2给去掉了

达到了我们想要的效果。

新增:2.类的某个属性是list数组,按照这个list的各元素的值是否相同来判断是否是同一个对象

我们来新增加一个比较器:

public class PeopleComparedByListValue : IEqualityComparer

{

public bool Equals(People x, People y)

{

if ( == null && == null)

return true;

if ( == null || == null)

return false;

if ( != )

return false;

//循环比较值

for (int i = 0; i < ; i++)

{

if ([i] != [i])

return false;

}

return true;

}

public int GetHashCode(People obj)

{

var hashCode = 1;

if ( == null)

return 0;

if ( == 0)

return 1;

(x => hashCode ^= x);

return hashCode;

}

}

使用这个比较器:

static void Main(string[] args)

{

List list = new List() { 1, 2, 11, 222, 3, 4 };

List list1 = new List() { 1, 2, 11, 222, 3, 4 };

People p1 = new People() { Name = "Tony1", ID = 1, Age = 18, Members = list };

People p2 = new People() { Name = "Tony2", ID = 2, Age = 19, Members = list };

People p3 = new People() { Name = "Tony3", ID = 3, Age = 20, Members = list1 };

People p4 = new People() { Name = "Tony4", ID = 4, Age = 21, Members = new List() };

List personList = new List() { p1, p2,p2,p3,p4 };

(new PeopleComparedByListValue()).ToList().ForEach(x => (x));

();

}

运行结果:

可以看到,数组值为1, 2, 11, 222, 3, 4 的只剩下一个了,达到了按值相同去重复的效果。

以后遇到以三个或者四个甚至更多的对象成员来决定对象是否重复的去重问题时,可以采用这种方法。

以上为个人拙见。这篇C# 实现Distinct将对象按条件去重就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。