简体   繁体   English

IEqualityComparer使用字符串列表作为比较器

[英]IEqualityComparer using list of string as comparer

I'm attempting to setup an IEqualityComparer that uses a list of string as the comparing property. 我正在尝试设置一个使用字符串列表作为比较属性的IEqualityComparer。

When using Except and Intersect in the 2 lines of code below, all records are seen as 'new' and none are recognized as 'old'. 在下面的两行代码中使用Except和Intersect时,所有记录都被视为“new”,并且没有一个被识别为“old”。

List<ExclusionRecordLite> newRecords = currentRecords.Except(historicalRecords, new ExclusionRecordLiteComparer()).ToList();
List<ExclusionRecordLite> oldRecords = currentRecords.Intersect(historicalRecords, new ExclusionRecordLiteComparer()).ToList();

This is my IEqualityComparer class (Words is a List) 这是我的IEqualityComparer类(单词是一个列表)

public class RecordComparer : IEqualityComparer<Record>
{
    public bool Equals(Record x, Record y)
    {
        if (object.ReferenceEquals(x, y))
            return true;

        if (x == null || y == null)
            return false;

        return x.Words.SequenceEqual(y.Words);
    }

    public int GetHashCode(Record obj)
    {
        return new { obj.Words }.GetHashCode();
    }
}

Your GetHashCode is incorrect. 您的GetHashCode不正确。 Use one like this: 使用这样的一个:

public override int GetHashCode()
{
    if(Words == null) return 0;
    unchecked
    {
        int hash = 19;
        foreach (var word in Words)
        {
            hash = hash * 31 + (word == null ? 0 : word.GetHashCode());
        }
        return hash;
    }
}

To answer why a collection does not override GetHashCode but uses object.GetHashCode which returns a unique value: Why does C# not implement GetHashCode for Collections? 要回答为什么集合不重写GetHashCode但使用object.GetHashCode返回唯一值的原因: 为什么C#没有为集合实现GetHashCode?

Assuming you're storing Words in a List then calling GetHashCode on it won't give you back a hash of the items in it, instead it'll give you back the hash value from Object.GetHashCode . 假设您在List存储Words ,那么在其上调用GetHashCode将不会返回其中的项的哈希,而是它将从Object.GetHashCode返回哈希值。

You need to implement your own hash function that enumerates over the words and generates a hash value. 您需要实现自己的哈希函数,该函数枚举单词并生成哈希值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 字典<string,string> (IEqualityComparer<string> ?comparer) 构造函数示例 - C# Dictionary<string,string>(IEqualityComparer<string>?comparer) constructor example GroupBy 和 IEqualityComparer<TKey> 比较器 - GroupBy and IEqualityComparer<TKey> comparer 获取不同的结果(IEqualityComparer比较器) - Getting the results of distinct( IEqualityComparer comparer) 正确实现IEqualityComparer for List <Dictionary<string,string> &gt; - Correct implementation of IEqualityComparer for List<Dictionary<string,string>> 从列表转换 <value> 到IEqualityComparer <string> - convert from List<value> to IEqualityComparer<string> 摘要IEqualityComparer实现或覆盖默认比较器以使用Distinct方法 - Abstract an IEqualityComparer implementation or override the default comparer to use Distinct method 为什么是List <T> 。使用Comparer排序 <int> 。默认速度是同等自定义比较器的两倍多? - Why is List<T>.Sort using Comparer<int>.Default more than twice as fast as an equivalent custom comparer? 使用Lambda OrderBy Comparer为特定字符串值赋予权重 - Using Lambda OrderBy Comparer to give weight to a specific string value 使用自定义比较器对 MM-YYYY 字符串进行排序 - Sorting MM-YYYY string using custom comparer 使用linq字符串比较器在标题单击时对Datagrid视图进行排序 - Datagrid view sort on header click, using linq string comparer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM