C# 集合交、并、差、去重,对象集合交并差
关键词:C# List 集合 交集、并集、差集、去重, 对象集合、 对象、引用类型、交并差、List<T>
有时候看官网文档是最高效的学习方式!
一、简单集合
Intersect 交集,Except 差集,Union 并集
int[] oldArray = { 1, 2, 3, 4, 5 };
int[] newArray = { 2, 4, 5, 7, 8, 9 };
var jiaoJi = oldArray.Intersect(newArray).ToList();//2,4,5
var oldChaJi = oldArray.Except(newArray).ToList();//1,3
var newChaJi = newArray.Except(oldArray).ToList();//7,8,9
var bingJi = oldArray.Union(newArray).ToList();//1,2,3,4,5,7,8,9
二、对象集合
Product[] store1 = { new Product { Name = "apple", Code = 9 }, new Product { Name = "orange", Code = 4 } }; Product[] store2 = { new Product { Name = "apple", Code = 9 }, new Product { Name = "lemon", Code = 12 } }; IEnumerable<Product> union =store1.Union(store2,new ProductComparer()); IEnumerable<Product> except=store1.Except(store2,new ProductComparer()); IEnumerable<Product> intersect=store1.Intersect(store2,new ProductComparer()); IEnumerable<Product> distinct=store1.Distinct(store2,new ProductComparer());
小提示:
1:IEnumerable<Product> 可以简化为 匿名类型 var
对自己去重:
var distinct=store1.Distinct(new ProductComparer());
相对于别人去重
var distinct=store1.Distinct(store2,new ProductComparer());
2: 可以继续进行一些linq或拉姆达操作
var distinct=store1.Distinct(store2,new ProductComparer()).OrderBy(c=>c.Code);
原因是引用了linq组件:using System.Linq;
三、比较类的实现
public class Product {
public string Id {get;set} public string Name { get; set; } public int Code { get; set; } }
1只有一个比较条件
//如果对象存在唯一主键,例如:从数据库里查询出来的数据存在 ID class ProductComparer : IEqualityComparer<Product> { // Products are equal if their names and product numbers are equal. public bool Equals(Product x, Product y) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; //Check whether the products' properties are equal. return x.ID == y.ID; } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public int GetHashCode(Product product) { //Check whether the object is null if (Object.ReferenceEquals(product, null)) return 0; //Get hash code for the Code field. int hashID = product.ID.GetHashCode(); //Calculate the hash code for the product. return hashID; } }
2 多个比较条件
// 如果存在组合主键或组合唯一索引,即多个字段组合才能确定唯一性。 // Custom comparer for the Product class class ProductComparer : IEqualityComparer<Product> { // Products are equal if their names and product numbers are equal. public bool Equals(Product x, Product y) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; //Check whether the products' properties are equal. return x.Code == y.Code && x.Name == y.Name; } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public int GetHashCode(Product product) { //Check whether the object is null if (Object.ReferenceEquals(product, null)) return 0; //Get hash code for the Name field if it is not null. int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode(); //Get hash code for the Code field. int hashProductCode = product.Code.GetHashCode(); //Calculate the hash code for the product. return hashProductName ^ hashProductCode; } }
3 难道我们每次都要“简单重复的”继承IEqualityComparer接口,来重新实现一个“几乎完全相同的”的Compare类吗?
对于只有一个比较条件的简单情况,我们可以直接对 Distinct、Union、Except、Intersect 进行封装,简单通用方法:
来源:C#list去重, https://www.cnblogs.com/hao-1234-1234/p/8855218.html
public class Compare<T, C> : IEqualityComparer<T>
{
private Func<T, C> _getField;
public Compare(Func<T, C> getfield)
{
this._getField = getfield;
}
public bool Equals(T x, T y)
{
return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
}
public int GetHashCode(T obj)
{
return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
}
}
public static class CommonHelper
{
/// <summary>
/// 自定义Distinct扩展方法
/// </summary>
/// <typeparam name="T">要去重的对象类</typeparam>
/// <typeparam name="C">自定义去重的字段类型</typeparam>
/// <param name="source">要去重的对象</param>
/// <param name="getfield">获取自定义去重字段的委托</param>
/// <returns></returns>
public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
{
return source.Distinct(new Compare<T, C>(getfield));
}
}
然后这么使用:store1.MyDistinct(s=>s.Id).ToList();
Id 是用于较的属性(或字段),它是可以是任何一个属性。
4、多个比较条件 的通用方法如何实现?
类似于 store1.MyDistinct(s=>s.Id&&Name).ToList();
四、微软官方文档
union :https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.union?view=netframework-4.8
intersect:https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.intersect?view=netframework-4.8
except:https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.except?view=netframework-4.8
distinct:https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8
合交并差 核心代码是相同的: IEqualityComparer 。
建议在看看文档其它部分,以备后用,文档写的很棒!
例如:join 某些情况与union等效。
五、交集、并集、差集、补集、对称差集 示意图,
后两者后可以有前三者演变而来。补集可以求差或去重、对称差集可以先求差、在求并;
参考过的文章
https://www.oschina.net/code/snippet_222150_16997
http://www.cnblogs.com/flywing/p/5912242.html
https://blog.csdn.net/wizblack/article/details/78796557
相关文章
- c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习
- C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
- 二维数组,锯齿数组和集合 C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用 C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- [C#基础]c#中的BeginInvoke和EndEndInvoke
- C# 字符串拼接性能探索 c#中+、string.Concat、string.Format、StringBuilder.Append四种方式进行字符串拼接时的性能
- Word处理控件Aspose.Words功能演示:在 C# 中将 Word DOC DOCX 转换为 Markdown
- C#学习记录——System.IO命名空间,文件基本操作
- CLR via C# (第三版)读后总结
- c# 文件夹权限
- 《C#多线程编程实战(原书第2版)》——3.8 使用BackgroundWorker组件
- C# 中线程同步使用信号量总结
- C#获取本机IP地址字符串
- C# 数组及集合
- C# Attribute
- [转]C#集合类型大揭秘
- C#中遍历各类数据集合的方法总结
- 《C#零基础入门之百识百例》(八十)类型转换汇总 -- 数字转换/引用转换/ 装箱拆箱
- C#-Task.Run 和 Task.Factory.StartNew 区别
- C# 集合-并发处理-锁OR线程
- C# 跨域 请求带cookie
- C#与.NET Framework c#编程语言,和java是一样的。(c#,java) -->javaweb,asp.net
- c#集合去重&排序常用方法