c#对象集合去重_C# 集合交、并、差、去重,对象集合交并差

本文详细介绍了C#中如何对简单集合及对象集合进行交集、并集、差集和去重操作。通过示例展示了如何使用Intersect、Except、Union和Distinct方法,并提供了自定义比较器的实现,以应对不同比较条件的去重需求。此外,还分享了如何实现一个通用的去重方法以及相关操作的官方文档链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键词:C#  List 集合 交集、并集、差集、去重, 对象集合、 对象、引用类型、交并差、List

有时候看官网文档是最高效的学习方式!

一、简单集合

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 union =store1.Union(store2,newProductComparer());

IEnumerable except=store1.Except(store2,newProductComparer());

IEnumerable intersect=store1.Intersect(store2,newProductComparer());

IEnumerable distinct=store1.Distinct(store2,new ProductComparer());

小提示:

1:IEnumerable 可以简化为 匿名类型 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{//Products are equal if their names and product numbers are equal.

public boolEquals(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 intGetHashCode(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.

returnhashID;

}

}

2 多个比较条件

//如果存在组合主键或组合唯一索引,即多个字段组合才能确定唯一性。//Custom comparer for the Product class

class ProductComparer : IEqualityComparer{//Products are equal if their names and product numbers are equal.

public boolEquals(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 intGetHashCode(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 : IEqualityComparer{

private Func_getField;

public Compare(Funcgetfield)

{

this._getField =getfield;

}

public boolEquals(T x, T y)

{

return EqualityComparer.Default.Equals(_getField(x), _getField(y));

}

public intGetHashCode(T obj)

{

return EqualityComparer.Default.GetHashCode(this._getField(obj));

}

}

public static classCommonHelper

{

///

/// 自定义Distinct扩展方法

///

/// 要去重的对象类

/// 自定义去重的字段类型

/// 要去重的对象

/// 获取自定义去重字段的委托

///

public static IEnumerable MyDistinct(this IEnumerable source, Funcgetfield)

{

return source.Distinct(new Compare(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等效。

五、交集、并集、差集、补集、对称差集 示意图,

74e6a239903ccfd9a2bed9a134427ea0.png

后两者后可以有前三者演变而来。补集可以求差或去重、对称差集可以先求差、在求并;

参考过的文章

https://www.oschina.net/code/snippet_222150_16997

http://www.cnblogs.com/flywing/p/5912242.html

https://blog.youkuaiyun.com/wizblack/article/details/78796557

C#中,`List`对象集合的分组和可以通过LINQ(语言集成查询)来实现。LINQ提供了一种简洁且强大的方式来处理集合数据。以下是一些常见的操作示例: ### 分组 假设我们有一个`List`集合,其中包含多个对象,每个对象有一个属性`Category`,我们希望根据`Category`属性进行分组。 ```csharp using System; using System.Collections.Generic; using System.Linq; public class Item { public string Name { get; set; } public string Category { get; set; } } public class Program { public static void Main() { List<Item> items = new List<Item> { new Item { Name = "Item1", Category = "A" }, new Item { Name = "Item2", Category = "B" }, new Item { Name = "Item3", Category = "A" }, new Item { Name = "Item4", Category = "C" } }; var groupedItems = items.GroupBy(item => item.Category); foreach (var group in groupedItems) { Console.WriteLine($"Category: {group.Key}"); foreach (var item in group) { Console.WriteLine($" - {item.Name}"); } } } } ``` ### 假设我们希望根据某个属性(如`Name`)对`List`集合进行。 ```csharp using System; using System.Collections.Generic; using System.Linq; public class Item { public string Name { get; set; } public string Category { get; set; } } public class Program { public static void Main() { List<Item> items = new List<Item> { new Item { Name = "Item1", Category = "A" }, new Item { Name = "Item2", Category = "B" }, new Item { Name = "Item1", Category = "C" }, new Item { Name = "Item3", Category = "A" } }; var distinctItems = items.GroupBy(item => item.Name) .Select(group => group.First()); foreach (var item in distinctItems) { Console.WriteLine($"Name: {item.Name}, Category: {item.Category}"); } } } ``` ### 综合示例 如果我们需要同时进行分组和,可以结合使用`GroupBy`和`Select`方法。 ```csharp using System; using System.Collections.Generic; using System.Linq; public class Item { public string Name { get; set; } public string Category { get; set; } } public class Program { public static void Main() { List<Item> items = new List<Item> { new Item { Name = "Item1", Category = "A" }, new Item { Name = "Item2", Category = "B" }, new Item { Name = "Item1", Category = "C" }, new Item { Name = "Item3", Category = "A" } }; var groupedDistinctItems = items.GroupBy(item => item.Category) .Select(group => group.First()); foreach (var item in groupedDistinctItems) { Console.WriteLine($"Category: {item.Category}, Name: {item.Name}"); } } } ``` 通过这些示例,你可以根据需要灵活地对`List`集合进行分组和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值