C#Distinct将对象按条件去重

更新时间:2023-07-11 18:50:38 阅读: 评论:0

C#Distinct将对象按条件去重
平时,我们将c#中的Distinct⼤多⽤于对数组去重,⼀般数组为基础的数据类型,例如 int,string.也可以⽤于对象去重,我们看看C#对Distinct⽅法的定义:
有重载,第⼀个参数都加了this,是拓展⽅法,有关拓展⽅法,请百度了解。
下⾯我们来研究下Distinct的对象去重,假设我们现在有⼀个People类:
public class People
{
描写春的古诗public int ID { get; t; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; t; }
/// <summary>
/// 所属省份
/// </summary>
public string Province { get; t; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; t; }
public override string ToString()
{
return string.Format("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<People> ListPeople = new List<People>()
{
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<People>苏州春节习俗
{
public bool Equals(People x, People y)
{
if (x == null || y == null)
return fal;
if (x.ID == y.ID)
return true;
el
return fal;
}
public int GetHashCode(People obj)
{
if (obj == null)
return 0;
el
return obj.ID.GetHashCode();
}
}
继承IEqualityComparer接⼝必须实现Equals和GetHashCode⽅法。
我们⽐较的时候,传⼊⼀个PeopleCompareByID 的实体即可:
ListPeople.Distinct(new PeopleCompareByID()).ToList().ForEach(x => Console.WriteLine(x));
运⾏结果如下:
达到了我们以ID去重的效果。
现在需求⼜变,ID和省份相同的算同⼀个⼈,要输出⼈的信息(相同的随便输出⼀个即可),这个时候,我们看到ID=0和Province="湖北"的存在重复,要将其去重,我们再来⼀个类,还是继承⾃IEqualityComparer:
备孕不能吃什么public class PeopleCompareByIDAndProvince : IEqualityComparer<People>
{
public bool Equals(People x, People y)
{
if (x == null || y == null)
return fal;
if (x.ID == y.ID&&x.Province==y.Province)
return true;
el
return fal;
}
public int GetHashCode(People obj)
{
if (obj == null)
return 0;
el
return obj.ID.GetHashCode()^obj.Province.GetHashCode();
}
}
同样,使⽤Distinct⽅法的时候,传⼊PeopleCompareByIDAndProvince 的实例:
ListPeople.Distinct(new PeopleCompareByIDAndProvince()).ToList().ForEach(x => Console.WriteLine(x));
运⾏后的结果如下:
新增:1.类的某个属性是list数组,按照这个list的引⽤是否相同来判断是否是同⼀个对象我们再来修改⼀下上⾯的代码如下:
class Program
{
static void Main(string[] args)
{
List<int> list =new List<int>(){1,2,11,222,3,4};
List<int> list1 =new List<int>(){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<int>()};
List<People> personList =new List<People>(){ p1, p2,p2,p3,p4 };
personList.Distinct(new PeopleComparedByList()).ToList().ForEach(x => Console.WriteLine(x));
Console.Read();
}
}
public class People
{
public int ID {get;t;}
/// <summary>
/// 姓名
/// </summary>
public string Name {get;t;}
/// <summary>
/// 所属省份
/// </summary>
public string Province {get;t;}
/// <summary>
张梦莹/// 年龄
/// </summary>
public int Age {get;t;}
private List<int> members =new List<int>();
public List<int> Members
{
get{return members;}
t{ members =value;}
}
public override string ToString()
{
return string.Format("ID:{0} Name:{1} Province:{2} Age:{3},Members:{4}", ID, Name, Province, Age,string.Join("-",this.Members.ToList()));
}
}
public class PeopleComparedByList : IEqualityComparer<People>
中国建筑艺术
{
佛汉拳
public bool Equals(People x, People y)
{
if(x.Members == y.Members)
return true;
el
return fal;
}
public int GetHashCode(People obj)
{
return obj.Members.GetHashCode();
}
}

本文发布于:2023-07-11 18:50:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1077384.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   参数   集合   数组   没有
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图