使用具有公差的IEqualityComparer GetHashCode [英] Using IEqualityComparer GetHashCode with a tolerance

查看:93
本文介绍了使用具有公差的IEqualityComparer GetHashCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现对日期比较有容忍度的IEqualityComparer.我还研究了这个问题.问题是我无法使用替代方法,因为我在LINQ .GroupJoin()中使用了IEqualityComparer.我尝试了一些允许容忍的实现.我可以使用Equals(),因为我有两个对象,但是我不知道如何实现GetHashCode().

I am trying to implement an IEqualityComparer that has a tolerance on a date comparison. I have also looked into this question. The problem is that I can't use a workaround because I am using the IEqualityComparer in a LINQ .GroupJoin(). I have tried a few implementations that allow for tolerance. I can get the Equals() to work because I have both objects but I can't figure out how to implement GetHashCode().

我的最佳尝试看起来像这样:

My best attempt looks something like this:

public class ThingWithDateComparer : IEqualityComparer<IThingWithDate>
{
    private readonly int _daysToAdd;

    public ThingWithDateComparer(int daysToAdd)
    {
        _daysToAdd = daysToAdd;
    }

    public int GetHashCode(IThingWithDate obj)
    {
        unchecked
        {
            var hash = 17;
            hash = hash * 23 + obj.BirthDate.AddDays(_daysToAdd).GetHashCode();
            return hash;
        }
    }

    public bool Equals(IThingWithDate x, IThingWithDate y)
    {
        throw new NotImplementedException();
    }
}

public interface IThingWithDate
{
    DateTime BirthDate { get; set; }
}

使用.GroupJoin()GetHashCode()之外构建HashTable时,它会将要添加到两个/所有对象的天数加起来.这是行不通的.

With .GroupJoin() building a HashTable out of the GetHashCode() it applies the days to add to both/all objects. This doesn't work.

推荐答案

从概念上讲,该问题是不可能的.您正在尝试以一种没有相等形式的方式来比较对象,而相等形式是您要尝试执行的相等操作所必需的.例如,GroupJoin取决于以下假设:如果A等于B,并且B等于C,那么A等于C,但是在您的情况下,这是不正确的. A和B可能足够靠近"在一起,您想对它们进行分组,但是A和C可能没有.

The problem is impossible, conceptually. You're trying to compare objects in a way that doesn't have a form of equality that is necessary for the operations you're trying to perform with it. For example, GroupJoin is dependant on the assumption that if A is equal to B, and B is equal to C, then A is equal to C, but in your situation, that's not true. A and B may be "close enough" together for you to want to group them, but A and C may not be.

您将根本不需要实现IEqualityComparer,因为您无法履行其所需的合同.如果您要创建一个集合中的项目到另一个集合中所有足够接近"的项目的映射,则您需要自己编写该算法(如此高效地执行可能很困难,但这样做效率不低不是那么困难),而不是使用GroupJoin,因为它无法执行该操作.

You're going to need to not implement IEqualityComparer at all, because you cannot fulfill the contract that it requires. If you want to create a mapping of items in one collection to all of the items in another collection that are "close enough" to it then you're going to need to write that algorithm yourself (doing so efficiently is likely to be hard, but doing so inefficiently isn't shouldn't' be that difficult), rather than using GroupJoin, because it's not capable of performing that operation.

这篇关于使用具有公差的IEqualityComparer GetHashCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆