- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 SQL Server 2008 中获取 guid.getHashCode() 的等效 int 值。我已经尝试过 CHECKSUM(uniqueidentifier var) 但它没有返回相同的值。是否可以在 SQL Server 中获取 guid 的 HashCode 值?
例如
Guid guid = new Guid("A0AE0446-3C50-479A-A909-3BA9C711007E");
int hash = guid.GetHashCode();,
返回-1476508766,是否可以在sql server中得到相同的值?
最佳答案
为此,您需要了解 Guid.GetHashCode()
如何计算该值。使用 DotPeek:
public override int GetHashCode()
{
return this._a ^ ((int) this._b << 16 | (int) (ushort) this._c) ^ ((int) this._f << 24 | (int) this._k);
}
私有(private)变量保存 Guid
的值并声明为:
private int _a;
private short _b;
private short _c;
private byte _d;
private byte _e;
private byte _f;
private byte _g;
private byte _h;
private byte _i;
private byte _j;
private byte _k;
作为 T-SQL 函数实现:
create function dbo.GetDotNetHashCode(@guid uniqueidentifier)
returns int
as
begin
-- calculates a hash code value equivalent to .NETs Guid.GetHashCode() method
declare @bytes binary(16) = cast(@guid as binary(16)),
@hashCode int
-- when converting to binary, the byte order of the first 3 segments is reversed
declare @_a int = convert(int,
substring(@bytes, 4, 1) +
substring(@bytes, 3, 1) +
substring(@bytes, 2, 1) +
substring(@bytes, 1, 1))
declare @_b int = convert(smallint,
substring(@bytes, 6, 1) +
substring(@bytes, 5, 1))
declare @_c int = convert(int,
substring(@bytes, 8, 1) +
substring(@bytes, 7, 1))
declare @_f int = convert(int, substring(@bytes, 11, 1))
declare @_k int = convert(int, substring(@bytes, 16, 1))
-- shift @_b using a bigint to avoid overflow
-- left shift 16 is equivalent to multiplying by 2^16
declare @_b_shift bigint = cast(@_b as bigint) * 65536
set @_b = convert(int, substring(cast(@_b_shift as varbinary(8)), 5, 4))
-- shift @_f using a bigin to avoid overflow
-- left shift 24 is equivalent to multiplying by 2^24
declare @_f_shift bigint = cast(@_f as bigint) * 16777216
set @_f = convert(int, substring(cast(@_f_shift as varbinary(8)), 5, 4))
set @hashCode = @_a ^ (@_b | @_c) ^ (@_f | @_k)
return @hashCode
end
并使用以下内容进行测试(注释具有调用 Guid.GetHashCode()
的预期值):
/*
aeee939b-d2c8-4411-9cf9-94ab520e3c1c : -400107626
88ed5159-7900-4530-9886-841b7d42665b : 1978471474
2910ba35-ab2f-42a5-aed1-ea710d25a42e : 1749022910
0430b8b0-981b-4f1a-82fb-afe0725e8fd3 : 858519417
ba8cd9e4-fa1d-4ead-9145-b830aea6a641 : -124676344
9d5b5203-a2d4-4b8e-86dd-7eb5e4250960 : 1099897325
28b88e48-acc4-4f74-be28-4fcb92ad8c55 : -881016471
892056e9-dde1-4999-92ba-6c74b4a583c2 : 952180658
f0c3d3b5-bffc-4bda-b45a-70825f33b01f : 1061132400
df218e55-d672-48aa-b105-47578869068d : 1314113138
*/
declare @test table (Guid uniqueidentifier)
insert @test values
('aeee939b-d2c8-4411-9cf9-94ab520e3c1c'),
('88ed5159-7900-4530-9886-841b7d42665b'),
('2910ba35-ab2f-42a5-aed1-ea710d25a42e'),
('0430b8b0-981b-4f1a-82fb-afe0725e8fd3'),
('ba8cd9e4-fa1d-4ead-9145-b830aea6a641'),
('9d5b5203-a2d4-4b8e-86dd-7eb5e4250960'),
('28b88e48-acc4-4f74-be28-4fcb92ad8c55'),
('892056e9-dde1-4999-92ba-6c74b4a583c2'),
('f0c3d3b5-bffc-4bda-b45a-70825f33b01f'),
('df218e55-d672-48aa-b105-47578869068d')
select Guid, dbo.GetDotNetHashCode(Guid) as HashCode from @test
注意:这是在 SQL Server 2012 中测试过的,但我相信它在 2008 年也能正常工作。
关于c# - 在 Sql Server 2008 中获取 Guid.getHashCode() 的等效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17550033/
我有一个实现 IEquatable<> 的类 A,使用它的字段(比如 A.b 和 A.c)来实现/覆盖 Equals() 并覆盖 GetHashCode(),并且一切正常,99% 的时间。 A 类是继
为什么会这样17m.GetHashCode() == 17d.GetHashCode()(m =十进制,d =两倍) 此外,如预期的那样17f.GetHashCode() != 17d.GetHash
我找到了一个 GetHashCode() 的实现,看起来像这样 Guid _hashCode = Guid.NewGuid(); public override int GetHash
我想使用 Distinct()使用我的数据,声明为 IEnumerable> .在这种情况下,我必须实现自己的 IEqualityComparer还有我的问题: 以下实现之间有什么区别吗? publi
我认为这些集合的 GetHashCode 函数不将它们的哈希码基于列表中的项目是很奇怪的。 我需要它来工作以提供脏检查(您有未保存的数据)。我已经编写了一个覆盖 GetHashCode 方法的包装类,
明明有更好的hash方法 有位朋友对我吐槽前几天我列举的在源生成器的生成db映射实体的优化点 提前生成部分 hashcode 进行比较 所示代码 public static void Gener
我正在为在 .net 上运行的语言编写编译器,我希望它做的一件事是自动生成 GetHashCode 方法,但我有几个问题: 这是否可能,编译器是否足够了解所涉及的类型以合理地实现该方法? 我应该为值类
昨天我浏览了一些 .net 源代码,看到了一些 GetHashcode 的实现,其中包含以下内容: (i1 << 5) + i ^ i2 我了解代码在做什么以及为什么。我想知道的是他们为什么使用 (i
对于 Delphi 项目(使用 RAD Studio XE7 构建),我想创建一个画笔字典。每个字典项都包含一个 TMyBrush 对象作为键,该对象描述要检索的画笔,以及一个 GDI+ 画笔作为值。
我有以下类(class): public class Foo { int year; string name; int category; } 这是一些示
我有这个问题。 public class Foo : object { public override bool Equals(obj a, objb) { return
有时我需要没有字段(消息头、模式等)的值对象,例如: abstract class RequestHeader { } sealed class FirstRequestHeader : Reques
我正在使用 Linq-to-Sql 查询 SQL Server 数据库。此查询返回我的数据库中实体的列表。我的基础数据没有改变。 收到列表后,我会对其调用 GetHashCode 以测试是否相等。奇怪
我在这里阅读了一些与 GetHashCode 正确实现相关的问题。我没有找到的是什么时候我应该实现这个方法。 在我的具体案例中,我构建了一个简单的不可变结构: public struct MyStru
我正在使用 unity,而 unity 中没有元组,所以我创建了自己的元组类来工作,因为我的字典需要它。 Dictionary , Tile> 我创建的 Tile 类与解决这个问题并不相关(至少我认为
我在 Microsoft 文档中找到了以下内容: Two objects that are equal return hash codes that are equal. However, the r
我试着关注 Guidelines来自 MSDN,也引用了 This great question但下面的行为似乎并不像预期的那样。 我试图表示类似于 FQN 的结构,其中就好像 P1 在 P2 之前列
我最近用几种不同的方式问过这个问题,但没有得到告诉我如何使用 的字典的答案。当我持有对更改的内容的引用时需要处理 T.GetHashCode() .出于此问题的目的,“状态”指的是在 Equals(
我有以下类(class) public class ResourceInfo { public string Id { get; set; } public string Url {
首先,我使用 GetHashCode算法描述,here .现在,想象以下(人为的)示例: class Foo { public Foo(int intValue, double doubleV
我是一名优秀的程序员,十分优秀!