背景说明
在EF等ORM框架中需要以List实体类的方式对数据进行大量操作,其中免不了对一些数据进行去重复,而C#中IEnumerable.Distinct()便提供了这一功能。只是对刚开始接触的新人来说比价抽象难以接受,本文会对这一功能进行简要说明,如果有更好的实现方式,也请大家畅所语言。
在写本文时,本人也在网上搜索了很多相关资料,其中有几篇比较有参考价值,也是重点,本文也是基于这几篇文章提供的代码进行优化和整理:
1、用泛型委托实现IEqualityComparer接口:https://blog.csdn.net/honantic/article/details/51595823
2、Distinct的多条件查询:https://blog.csdn.net/lishuangquan1987/article/details/76096022
3、IEqualityComparer中的Equal()和GetHashCode():https://www.cnblogs.com/xiaochen-vip8/p/5506478.html
我们要做的是实现IEqualityComparer()接口,而且必须要用泛型,因为我们希望这个功能是可以对所有实体类实现的。其中对哈希值的了解可以参考第三条链接,可以简单的概括为,哈希值反应的是对象在内存中的地址,只有地址相同的对象才能激活IEqualityComparer中的Equal()方法,Equal()可以根据自己的需求而实现。话不多说,代码如下:
/// <summary>/// 用委托实现IEqualityComparer<T>接口/// </summary>/// <typeparam name="T">目标类型</typeparam>public class ListComparer<T> : IEqualityComparer<T>{public Func<T, T, bool> EqualsFunc;public Func<T,int> GetHashCodeFunc;public ListComparer(Func<T,T,bool> Equals, Func<T,int> GetHashCode){this.EqualsFunc = Equals;this.GetHashCodeFunc = GetHashCode;}public ListComparer(Func<T, T, bool> Equals) : this(Equals, t => 0){}public bool Equals(T x, T y){if (this.EqualsFunc != null){return this.EqualsFunc(x, y);}else{return false;}}/// <summary>/// 获取目标对象的哈希值,只有返回相同的哈希值才能运行Equals方法/// </summary>/// <param name="obj">获取哈希值的目标类型对象</param>/// <returns>返回哈希值</returns>public int GetHashCode(T obj){if (this.GetHashCodeFunc != null){return this.GetHashCodeFunc(obj);}else{return 0;}}}
以上代码中,默认哈希值是相同的,我们开始看看使用效果,代码如下:
static void Main(string[] args){List<Phone> PhoneLists = new List<Phone>(){new Phone { Country = "中国", City = "北京", Name = "小米" },new Phone { Country = "中国",City = "北京",Name = "华为"},new Phone { Country = "中国",City = "北京",Name = "联想"},new Phone { Country = "中国",City = "台北",Name = "魅族"},new Phone { Country = "日本",City = "东京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "美国",City = "加州",Name = "苹果"},new Phone { Country = "美国",City = "华盛顿",Name = "三星"}};var Lists = PhoneLists.Distinct<Phone>();foreach (var list in Lists){Console.WriteLine(list.Country + "-" + list.City + "-" + list.Name);}Console.Read();}
在Distinct()方法没有任何参数的情况下,运行后如下图所示:
我们可以看到,好像并没有任何效果,但是其实是有效果的,因为每个Phone实体类对象在内存中的地址是不一样的, Distinct()方法默认筛选出所有内存地址不一样的实体类对象。
接下去需求改变,我们希望得出总共有多少个不同的country,country相同的数据随便返回其中一个就行,代码如下所示:
static void Main(string[] args){List<Phone> PhoneLists = new List<Phone>(){new Phone { Country = "中国", City = "北京", Name = "小米" },new Phone { Country = "中国",City = "北京",Name = "华为"},new Phone { Country = "中国",City = "北京",Name = "联想"},new Phone { Country = "中国",City = "台北",Name = "魅族"},new Phone { Country = "日本",City = "东京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "美国",City = "加州",Name = "苹果"},new Phone { Country = "美国",City = "华盛顿",Name = "三星"}};var Lists2 = PhoneLists.Distinct<Phone>(new ListComparer<Phone>((x,y) => x.Country.Equals(y.Country)));foreach (var list in Lists){Console.WriteLine(list.Country + "-" + list.City + "-" + list.Name);}Console.Read();}
我们对country字段进行去重,得到的结果如下图所示:
再接下去,需求又变,我们要筛选出有多少不同的国家和城市,这意味着要对country和city两个字段进行去重,代码如下:
static void Main(string[] args){List<Phone> PhoneLists = new List<Phone>(){new Phone { Country = "中国", City = "北京", Name = "小米" },new Phone { Country = "中国",City = "北京",Name = "华为"},new Phone { Country = "中国",City = "北京",Name = "联想"},new Phone { Country = "中国",City = "台北",Name = "魅族"},new Phone { Country = "日本",City = "东京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "美国",City = "加州",Name = "苹果"},new Phone { Country = "美国",City = "华盛顿",Name = "三星"}};var Lists = PhoneLists.Distinct<Phone>(new ListComparer<Phone>((x, y) => x.Country.Equals(y.Country) && x.City.Equals(y.City)));foreach (var list in Lists){Console.WriteLine(list.Country + "-" + list.City + "-" + list.Name);}Console.Read();}
执行结果如下图所示:
可以看到,已经达到了多字段的去重复效果,即便遇到需要去重复多个字段也可以实现,以上为个人拙见。