如何比较C#/ SQL投领域rowversion数据 [英] How compare/cast SQL rowversion field data in C#
问题描述
我在我的学生表两个记录,一个行版本字段。 。我创造这个纪录在我的C#代码学生类的对象
我的问题是:
- 在其中C#兼容的数据类型为rowversion数据投?
- 如何比较的两个记录rowversion并找出哪一个是在C#? 最新
要找到最新的rowversion的一种方法是使用的 StructuralComparer
VAR R1 =新的字节[] {0×00,0×00,0×00,0×00,0×00,0×00,0×10,0×00};
变种R2 =新的字节[] {0×00,0×00,0×00,0×00,0×00,0×00,×09,0xFF的};
布尔r1_is_newer = StructuralComparisons.StructuralComparer.Compare(R1,R2)与GT; 0;
结构比较的一个好处是,它们会自动处理 NULL
取值
关于利用 BitConverter
。如果上面的字节数组中使用的 UINT64 的.aspx> BitConverter
,你最终会具有非常高的数字和比较是没有意义的(由一个rowversion变化,UINT64转换数量的变化通过大约2 ^ 56)
如果您想撒一些LINQ过它,使其更慢,你可以用
布尔r1_is_newer =
Enumerable.Zip(R1,R2,(B1,B2)=>新建{B1,B2})
。 SkipWhile(项目=> item.b1 == item.b2)
。取(1)。任何(项目=> item.b1> item.b2);
I have two records in my student table with a row-version field. I am creating the object of that record as student class in my C# code.
My question are:
- In which C# compatible datatype is rowversion data cast?
- How do I compare rowversion of two records and find which one is the latest in C#?
One way to find the latest rowversion is to use the 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;
A benefit of the structural comparisons is that they automatically deal with NULL
s
Regarding use of the BitConverter
. If the above byte arrays were converted to UInt64
using the BitConverter
, you'd end up with very high numbers and the comparison is meaningless (as the rowversion changes by one, the uint64 converted number changes by around 2^56)
If you'd like to sprinkle some LINQ over it to make it extra slow, you can use
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#/ SQL投领域rowversion数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!