软件编程
位置:首页>> 软件编程>> C#编程>> c# Linq distinct不会调用Equals方法详解

c# Linq distinct不会调用Equals方法详解

作者:程序园  发布时间:2021-09-03 09:53:13 

标签:c#,Linq,distinct,Equals

我有以下课程


public class ModInfo : IEquatable<ModInfo>
{
public int ID { get; set; }
public string MD5 { get; set; }

public bool Equals(ModInfo other)
{
 return other.MD5.Equals(MD5);
}

public override int GetHashCode()
{
 return MD5.GetHashCode();
}
}

我使用如下方法将一些数据加载到该类的列表中:


public void ReloadEverything() {
var beforeSort = new List<ModInfo>();
// Bunch of loading from local sqlite database.
// not included since it's reload boring to look at
var modinfo = beforeSort.OrderBy(m => m.ID).AsEnumerable().Distinct().ToList();
}

问题是Distinct()调用似乎没有做到这一点.仍然存在彼此相等的对象.

根据这篇文章:https://msdn.microsoft.com/en-us/library/vstudio/bb348436%28v=vs.100%29.aspx

这就是你应该如何做出不同的工作,但它似乎并没有在ModInfo对象上调用Equals方法.

是什么导致这种情况发生?

示例值:

modinfo[0]: id=2069, MD5 =0AAEBF5D2937BDF78CB65807C0DC047C

modinfo[1]: id=2208, MD5 = 0AAEBF5D2937BDF78CB65807C0DC047C

我不关心选择哪个值,因为md5值相同,它们可能是相同的.

您还需要覆盖Object.Equals,而不仅仅是实现IEquatable.

如果你把它添加到你的班级:


public override bool Equals(object other)
{
ModInfo mod = other as ModInfo;
if (mod != null)
 return Equals(mod);
return false;
}

它应该工作.

有关详细信息,请参阅此文章:Implementing IEquatable Properly

编辑:好的,这是基于GetHashCode的最佳实践的略有不同的实现.


public class ModInfo : IEquatable<ModInfo>
{
public int ID { get; set; }
public string MD5 { get; set; }

public bool Equals(ModInfo other)
{
 if (other == null) return false;
 return (this.MD5.Equals(other.MD5));
}

public override int GetHashCode()
{
 unchecked
 {
  int hash = 13;
  hash = (hash * 7) + MD5.GetHashCode();
  return hash;
 }
}

public override bool Equals(object obj)
{
 ModInfo other = obj as ModInfo;
 if (other != null)
 {
  return Equals(other);
 }
 else
 {
  return false;
 }
}
}

你可以验证它:


ModInfo mod1 = new ModInfo {ID = 1, MD5 = "0AAEBF5D2937BDF78CB65807C0DC047C"};
ModInfo mod2 = new ModInfo {ID = 2, MD5 = "0AAEBF5D2937BDF78CB65807C0DC047C"};

// You should get true here
bool areEqual = mod1.Equals(mod2);

List<ModInfo> mods = new List<ModInfo> {mod1, mod2};

// You should get 1 result here
mods = mods.Distinct().ToList();

What's with those specific numbers in GetHashCode?

补充知识:C#中通过Distinct方法对List集合进行去重

在C#的List集合对象中,可以使用Distinct方法来对List集合元素进行去重,如果list集合内部元素为值类型,则Distinct方法根据值类型是否相等来判断去重,如果List集合内部元素为引用类型变量,则是判断相同引用的对象为相同进行List集合元素去重操作。

(1)值类型的List集合对象intList,内部元素为1,1,2,2,3,4,5等这几个元素。对intList对象进行去重可使用下列语句:

List intList= new List() { 1, 1,2,2,3,4,5};

intList= intList.Distinct().ToList();

经过上述Distinct方法去重处理并重新赋值后,intList集合内部元素为:1,2,3,4,5。

(2)针对引用类型的Distinct方法去重,则是判断List集合中的对象引用地址是否一致,不一致的话为不同的两个对象,即使2个对象的每个属性值都一样的情况下。


List testList = new List<ConsoleApplication1.TestModel>();
testList.Add(new TestModel()
{
Index=1,
Name=“Index1”
});
testList.Add(new TestModel()
{
Index = 2,
Name = “Index2”
});
testList.Add(new TestModel()
{
Index = 2,
Name = “Index2”
});
testList = testList.Distinct().ToList();

上述程序语句中,虽然List集合testList中的第2个元素和第3个元素的属性值完全一样,但这2个元素依旧是不同的对象,因此在调用Distinct()方法去重后重新赋值,testList依旧不变。

来源:http://www.voidcn.com/article/p-vdvfegie-bwm.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com