实现IEqualityComparer

我想从列表中区分对象。 我尝试实现IEqualityComparer但没有成功。 如果有人可以查看我的代码并给我一些关于IEqualityComparer的解释,请。 我有以下情况:

public class Message { public int x { get; set; } public string y { get; set; } public string z { get; set; } public string w { get; set; } } public class MessageComparer : IEqualityComparer { public bool Equals(Message x, Message y) { if (Object.ReferenceEquals(x, y)) return true; if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; if (xx == yx && xy == yy && xz == yz && xw == yw) { return true; } return false; } public int GetHashCode(Message number) { //if (Object.ReferenceEquals(number, null)) return 0; int hashX = number.x.GetHashCode(); int hashY = number.y == null ? 0 : number.y.GetHashCode(); int hashZ = number.z == null ? 0 : number.z.GetHashCode(); int hashW = number.w == null ? 0 : number.w.GetHashCode(); return hashX ^ hashY ^ hashZ ^ hashW; } } 

这是我的Message对象列表:

 Message m1 = new Message(); m1.x = 1; m1.y = "A"; m1.z = "B"; m1.w = "C"; Message m2 = new Message(); m2.x = 1; m2.y = "A"; m2.z = "B"; m2.w = "C"; Message m3 = new Message(); m3.x = 1; m3.y = "A"; m3.z = "B"; m3.w = "C"; Message m4 = new Message(); m4.x = 2; m4.y = "A"; m4.z = "B"; m4.w = "C"; Message m5 = new Message(); m5.x = 3; m5.y = "W"; m5.z = "D"; m5.w = "C"; Message m6 = new Message(); m6.x = 4; m6.y = "S"; m6.z = "F"; m6.w = "R"; List collection = new List(); collection.Add(m1); collection.Add(m2); collection.Add(m3); collection.Add(m4); collection.Add(m5); collection.Distinct(new MessageComparer()); 

当我调用Distinct()方法时,列表中的元素数量是相同的。 谢谢。

试试这个:

 var distinct = collection.Distinct(new MessageComparer()); 

然后在那之后使用distinct来做任何事情。

看起来你忘记了IEnumerable<>IEnumerable<> 。 LINQ方法都没有实际更改原始变量。 相反,它们返回包含表达式结果的IEnuerable 。 例如,让我们考虑一个简单的List original ,内容为{ "a", "a", "b", "c" }

现在,让我们调用original.Add("d"); 。 该方法没有返回值(它是void )。 但是如果我们打印出original的内容,我们会看到{ "a", "a", "b", "c", "d" }

另一方面,我们现在调用original.Skip(1) 。 此方法确实有一个返回值,类型为IEnumerable 。 它是一个LINQ表达式,不对原始集合执行任何副作用操作。 因此,如果我们称之为并看original ,我们将看到{ "a", "a", "b", "c", "d" } 。 但是,该方法的结果将是{ "a", "b", "c", "d" } 。 如您所见,结果会跳过一个元素。

这是因为LINQ方法接受IEnumerable作为参数。 因此,他们没有实施原始清单的概念。 您可以通过扩展方法传递ReadOnlyCollection ,他们仍然可以通过它进行评估。 因此,它们不能改变原始集合,因为原始集合可以以多种方式编写。

所有这一切,但以表格forms。 每行以原始{ "a", "a", "b", "c" }开头:

 Context Example function Immutable? Returned Value Collection after calling Collection Add("d") No (void) { "a", "a", "b", "c", "d" }: LINQ Skip(1) Yes { "a", "b", "c" } { "a", "a", "b", "c" }: 

您不需要实现IEqualityComparer:

  public class Message { protected bool Equals(Message other) { return string.Equals(x, other.x) && string.Equals(y, other.y) && string.Equals(z, other.z) && string.Equals(w, other.w); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; return Equals((Message) obj); } public override int GetHashCode() { unchecked //Ignores overflows that can (should) occur { var hashCode = x; hashCode = (hashCode*397) ^ (y != null ? y.GetHashCode() : 0); hashCode = (hashCode*397) ^ (z != null ? z.GetHashCode() : 0); hashCode = (hashCode*397) ^ (w != null ? w.GetHashCode() : 0); return hashCode; } } public int x { get; set; } public string y { get; set; } public string z { get; set; } public string w { get; set; } } 

有关更多详细信息,请参阅此链接

http://dotnetvisio.blogspot.in/2015/12/usage-of-icomparer-icomparable-and.html

IEqualityComparer是一个用于查找对象的接口,无论它是否为Equal,现在我们将在一个示例中看到这一点,我们必须在集合中找到Object的Distinct。 该接口将实现一个方法Equals(T obj1,T obj2)

abstract public class Person {public string FirstName {get; 组; public字符串LastName {get; 组; public字符串地址{set; 得到; }}

 public enum SortType { ByID, BySalary } public class EmployeeDistinctEquality : IEqualityComparer { public EmployeeDistinctEquality() { } public bool Equals(Employee x, Employee y) { if (x == null && x == null) return true; else if (x == null || y == null) return false; else if (x.Id == y.Id) return true; else return false; } public int GetHashCode(Employee obj) { return obj.Id.GetHashCode(); } } 

有关更多详细信息,请参阅此链接

http://dotnetvisio.blogspot.in/2015/12/usage-of-icomparer-icomparable-and.html