如何使用的IEqualityComparer [英] How to use the IEqualityComparer

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

问题描述

我有一些钟声在我与相同数量的数据库。我想所有的人都没有重复。然后,我创建了一个比较类做这项工作,但功能的执行使得从功能大不延误不同,从0.6秒到3.2秒!

我这样做是正确的或者我必须使用另一种方法?

  reg.AddRange((来自于this.dataContext.reglements
                     JOIN B在this.dataContext.Clients上a.Id_client等于b.Id
                     其中,a.date_v< = datefin和放大器;&安培; a.date_v> = datedeb
                     其中,a.Id_client == b.Id
                     排序依据a.date_v下降
                     选择新Class_reglement
                     {
                         NOM = b.Nom,
                         code = B。code,
                         Numf = a.Numf,
                     })AsEnumerable()鲜明(新比较(。))了ToList())。。
    类比较:&的IEqualityComparer LT; Class_reglement>
    {
        公共布尔等于(Class_reglement X,Class_reglement Y)
        {
            如果(x.Numf == y.Numf)
            {
                返回true;
            }
            其他{返回false; }
        }
        公众诠释GetHash code(Class_reglement codeH)
        {
            返回0;
        }    }


解决方案

这也难怪,考虑到你的 GetHash code 实现,它总是返回相同的值。 鲜明依赖于好的哈希函数高效地工作。

在您的code,解决的办法是转发 GetHash code Class_reglement.Numf.GetHash code 并适当地有实现它。

此外,你的等于 code是可憎的,应该是COM pressed到一个前pression:

 类比较:&的IEqualityComparer LT; Class_reglement>
{
    公共布尔等于(Class_reglement X,Class_reglement Y)
    {
        返回x.Numf == y.Numf;
    }
    公众诠释GetHash code(Class_reglement codeH)
    {
        返回codeh.Numf.GetHash code();
    }
}

此外,你的了ToList 通话是不必要的,费时的。 的AddRange 接受任何的IEnumerable 所以转换为列表 ISN需要吨。 AsEnumerable 的冗余在这里,因为处理结果在的AddRange 无论如何都会导致此。

所有的一切,似乎很多code被不理解其实际作用(这被称为货物书面邪教编程)。这似乎暂时工作,但它不工作的长期:采取超时和尝试的了解的你正在写,preferably而在C#读一本书

I have some bells in my database with the same number. I want to get all of them without duplication. Then I create a compare class to do this work, but the execution of the function makes a big delay from the function without distinct, from 0.6 sec to 3.2 sec!

Am I doing it right or I have to use another method?

       reg.AddRange((from a in this.dataContext.reglements
                     join b in this.dataContext.Clients on a.Id_client equals b.Id
                     where a.date_v <= datefin && a.date_v >= datedeb
                     where a.Id_client == b.Id
                     orderby a.date_v descending 
                     select new Class_reglement
                     {
                         nom = b.Nom,
                         code = b.code,
                         Numf = a.Numf,
                     }).AsEnumerable().Distinct(new Compare()).ToList());


    class Compare : IEqualityComparer<Class_reglement>
    {
        public bool Equals(Class_reglement x, Class_reglement y)
        {
            if (x.Numf == y.Numf)
            {
                return true;
            }
            else { return false; }
        }
        public int GetHashCode(Class_reglement codeh)
        {
            return 0;
        }

    }

解决方案

No wonder, considering your GetHashCode implementation which always returns the same value. Distinct relies on a good hash function to work efficiently.

In your code, the solution is to forward GetHashCode to Class_reglement.Numf.GetHashCode and implement it appropriately there.

Furthermore, your Equals code is an abomination that should be compressed into one expression:

class Compare : IEqualityComparer<Class_reglement>
{
    public bool Equals(Class_reglement x, Class_reglement y)
    {
        return x.Numf == y.Numf;
    }
    public int GetHashCode(Class_reglement codeh)
    {
        return codeh.Numf.GetHashCode();
    }
}

Furthermore, your ToList call is unnecessary and time-consuming. AddRange accepts any IEnumerable so conversion to a List isn’t required. AsEnumerable is also redundant here since processing the result in AddRange will cause this anyway.

All in all, it seems that a lot of code was written without understanding what it actually does (this is called cargo cult programming). This may seem to work temporarily but it doesn’t work long-term: take a time-out and try to understand what you are writing, preferably while reading a book on C#.

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

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