相关疑难解决方法(0)

重写System.Object.GetHashCode的最佳算法是什么?

在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?

.net algorithm hashcode gethashcode

1389
推荐指数
14
解决办法
19万
查看次数

为什么在重写Equals方法时重写GetHashCode很重要?

鉴于以下课程

public class Foo
{
    public int FooId { get; set; }
    public string FooName { get; set; }

    public override bool Equals(object obj)
    {
        Foo fooItem = obj as Foo;

        if (fooItem == null) 
        {
           return false;
        }

        return fooItem.FooId == this.FooId;
    }

    public override int GetHashCode()
    {
        // Which is preferred?

        return base.GetHashCode();

        //return this.FooId.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经覆盖了该Equals方法,因为它Foo代表了Foos表的一行.哪个是覆盖的首选方法GetHashCode

覆盖为什么重要GetHashCode

c# overriding hashcode

1371
推荐指数
13
解决办法
35万
查看次数

跳过/忽略插入时的重复行

我有以下表格:

DataValue

DateStamp    ItemId   Value
----------   ------   -----
2012-05-22   1        6541
2012-05-22   2        12321
2012-05-21   3        32
Run Code Online (Sandbox Code Playgroud)

tmp_holding_DataValue

DateStamp    ItemId   Value
----------   ------   -----
2012-05-22   1        6541
2012-05-22   4        87
2012-05-21   5        234
Run Code Online (Sandbox Code Playgroud)

DateStamp并且ItemId是主要的关键列.

我正在做一个插件,它会在一天中定期运行(在存储过程中):

insert into DataValue(DateStamp, ItemId, Value)
select DateStamp, ItemId, Value from tmp_holding_DataValue;
Run Code Online (Sandbox Code Playgroud)

这会将数据从保持表(tmp_holding_DataValue)移动到主数据表(DataValue)中.然后截断保持表.

问题是,如示例中所示,保持表可以包含主表中已存在的项.由于密钥不允许重复值,因此过程将失败.

一种选择是在insert proc上放置一个where子句,但主数据表有1000万+行,这可能需要很长时间.

是否还有其他方法可以让程序在尝试插入时跳过/忽略重复项?

t-sql sql-server stored-procedures sql-server-2008

25
推荐指数
3
解决办法
6万
查看次数