C#如何删除列表中的重复对象-对象本身很复杂,也有列表
我有一个c#list ComplexTypeObj 类ComplexTypeObj-具有以下3个属性 搜索=新列表(复杂对象) Ids=新列表(guid类型) 姓氏=列表(字符串类型)C#如何删除列表中的重复对象-对象本身很复杂,也有列表,c#,linq,C#,Linq,我有一个c#list ComplexTypeObj 类ComplexTypeObj-具有以下3个属性 搜索=新列表(复杂对象) Ids=新列表(guid类型) 姓氏=列表(字符串类型) 我需要一种方法来找出列表中是否有重复的ComplexTypeObj,其中每个属性都匹配,但我在这一个属性的逻辑上有点问题?这可能有点过分,但我最喜欢的比较复杂对象的库是CompareneObjects (GitHub:) (NuGet:安装软件包CompareNet对象-版本4.73.0) 下面是一个.NET小提
我需要一种方法来找出列表中是否有重复的ComplexTypeObj,其中每个属性都匹配,但我在这一个属性的逻辑上有点问题?这可能有点过分,但我最喜欢的比较复杂对象的库是CompareneObjects (GitHub:) (NuGet:安装软件包CompareNet对象-版本4.73.0) 下面是一个.NET小提琴手的工作示例:
//这是比较类
var compareLogic=新的compareLogic();
//(可选地为CompareLogic定义一些配置)
//例如,比较不同类型、忽略道具、区分大小写等。
compareLogic.Config.IgnoreProperty(x=>x.Searches);
compareLogic.Config.TreatStringEmptyAndNullTheSame=true;
compareLogic.Config.CaseSensitive=true;
//比较结果
var compareResults=compareLogic.Compare(cto3,cto1);//魔法发生的地方。
//检查对象是否相等
如果(!compareResults.AreEqual)
{
//做点什么。。。
}
但除了检查它们是否相等之外,返回的
ComparisonResult
对象将允许您查看对象属性之间的个别差异,或者获取所有属性差异的字符串,就像几个示例一样。在ComplexTypeObj类上实现IEquatable
,并在列表上使用HashSet.SetEquals
。etc.IEquatable和GetHashcode;)谢谢,我已经为equals public bool equals([AllowNull]ComplexTypeObj other)实现了Iequatable{返回this.Searches.equals(other.Searches)&&this.Ids.equals(other.Ids)&&this.names equals(other.names);}如何使用HashSet.SetEquals?
// This is the comparison class
var compareLogic = new CompareLogic();
// (Optionally define some configurations for the CompareLogic)
// e.g. Compare different types, Ignore props, Case sensitivity, etc.
compareLogic.Config.IgnoreProperty<ComplexTypeObj>(x => x.Searches);
compareLogic.Config.TreatStringEmptyAndNullTheSame = true;
compareLogic.Config.CaseSensitive = true;
// Compare the results
var compareResults = compareLogic.Compare(cto3, cto1); // Where the magic happens.
// Check if objects are equal
if(!compareResults.AreEqual)
{
// Do something...
}