相当于 C# 中的 SQL 二进制相对值测试

发布于 2024-08-19 03:57:40 字数 238 浏览 2 评论 0原文

我试图找出此 SQL 的等效 C#:

@var1 = "1a1"
@var2 = "1F"
CONVERT(varbinary, @var1) > CONVERT(varbinary, @var2)

它与此相同吗?

if (var1.CompareTo(var2) > 0)
{

}

如果不是,那我该如何模拟呢?

I am trying to figure out the equivalent C# for this SQL:

@var1 = "1a1"
@var2 = "1F"
CONVERT(varbinary, @var1) > CONVERT(varbinary, @var2)

Is it the same as this?

if (var1.CompareTo(var2) > 0)
{

}

If not, then how do I simulate it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2024-08-26 03:57:40

转换为 varbinary 可能是为了强制进行区分大小写的比较。如果这是唯一的问题,那么是的,这两个陈述是等效的。

默认字符串比较区分大小写。但是,默认字符串比较将使用当前区域性信息,并且可能会根据区域性以不同方式处理某些字符串。如果您的应用程序担心这一点,那么您可以使用序数比较,这将产生与 varbinary 强制转换完全相同的结果。

if (String.Compare(var1, var2, StringComparison.Ordinal) > 0)
{

}

Conversion to varbinary is presumably to force a case-sensitive comparison. If that is the only concern, then yes, the two statements are equivalent.

The default string comparison will be case sensitive. However, the default string comparison will use current culture information and may treat some strings differently depending on culture. If this is a concern for your application, then you can use ordinal comparison instead which would produce exactly the same results a the varbinary cast.

if (String.Compare(var1, var2, StringComparison.Ordinal) > 0)
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文