gpt4 book ai didi

c# - 如何在 C# 中比较/转换 SQL rowversion 字段数据

转载 作者:太空狗 更新时间:2023-10-29 17:42:25 28 4
gpt4 key购买 nike

我的学生表中有两 strip 有行版本字段的记录。我在我的 C# 代码中将该记录的对象创建为学生类。

我的问题是:

  1. rowversion 数据在哪个 C# 兼容数据类型中转换?
  2. 如何在 C# 中比较两条记录的行版本并找出哪一条是最新的?

最佳答案

查找最新行版本的一种方法是使用 StructuralComparer

var r1 = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00};
var r2 = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0xFF};

bool r1_is_newer = StructuralComparisons.StructuralComparer.Compare(r1, r2) > 0;

结构比较的一个好处是它们会自动处理NULL

关于 BitConverter 的使用。如果使用 BitConverter 将上述字节数组转换为 UInt64 ,你最终会得到非常高的数字并且比较没有意义(因为 rowversion 改变了一个,uint64 转换后的数字改变了大约 2^56)

如果你想在上面添加一些 LINQ 来让它变得特别慢,你可以使用

bool r1_is_newer = 
Enumerable.Zip(r1, r2, (b1, b2) => new { b1, b2 })
.SkipWhile(item => item.b1 == item.b2)
.Take(1).Any(item => item.b1 > item.b2);

关于c# - 如何在 C# 中比较/转换 SQL rowversion 字段数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7713143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com