的IEqualityComparer并不如预期IEqualityComparer

2023-09-03 03:24:52 作者:与孤独合葬

我有一个列表存储在我的计算机上的文件的路径。我的目标是首先过滤掉具有相同的名称和,然后过滤出那些具有相同的尺寸的文件。 要做到这一点,我已经做了两个班实施的IEqualityComparer<字符串> ,实施等于 GetHash code 的方法。

I have a List of paths of files stored on my computer. My aim is to first filter out the files which have the same name and and then filter out those which have the same size. To do so, I have made two classes implementing IEqualityComparer<string>, and implemented Equals and GetHashCode methods.

var query = FilesList.Distinct(new CustomTextComparer())
                     .Distinct(new CustomSizeComparer()); 

在code这两个类是如下: -

The code for both of the classes is given below:-

public class CustomTextComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        if (Path.GetFileName(x) == Path.GetFileName(y))
        {
            return true;
        }
        return false; 
    }
    public int GetHashCode(string obj)
    {
        return obj.GetHashCode();
    }
}
public class CustomSizeComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        if (new FileInfo(x).Length == new FileInfo(y).Length)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public int GetHashCode(string obj)
    {
        return obj.GetHashCode();
    }
}

不过,code不起作用。

But the code does not work.

这不会引发任何异常,也没有任何编译器错误,但问题是,code不工作(不排除重复文件)。

It doesn't throw any exceptions nor is there any compiler error, but the problem is that the code doesn't work(doesn't exclude duplicate files).

所以,我怎样才能解决这个问题?有什么我能做的正确使code的工作。

So, how can I correct this problem? Is there anything I can do to make the code work correctly.

推荐答案

更​​改 GetHash code 工作,对比较值。即为你的尺寸比较器:

Change your GetHashCode to work on the compared value. I.e. for your size comparer:

public int GetHashCode(string obj)
{
    return FileInfo(x).Length.GetHashCode();
}

而对于其他的:

And for the other:

public int GetHashCode(string obj)
{
    return Path.GetFileName(obj).GetHashCode();
}

根据这个答案 - What's的GetHash code在的IEqualityComparer&LT的作用; T&GT;在.NET?,散列code先评估。 等于就是所谓的碰撞的情况下。

According to this answer - What's the role of GetHashCode in the IEqualityComparer<T> in .NET?, the hash code is evaluated first. Equals is called in case of collision.

显然,这将是明智的工作,的FileInfo S,而不是字符串。

Obviously it would be sensible to work on FileInfos, not on strings.

所以,也许:

FileList.Select(x => new FileInfo(x))
        .Distinct(new CustomTextComparer())
        .Distinct(new CustomSizeComparer());

当然,你必须改变你的comparers工作,对正确的类型。

Of course, then you have to change your comparers to work on the correct type.