问题描述
我需要在由 Linq2sql 完成的查询中比较二进制值.
I need to compare binary values in a query done by Linq2sql.
table1.FirstOrDefault(r => r.ID.SequenceEqual(id))//ID and id are binary/byte[].
但是Linq2sql抛出异常,因为SequenceEqual
不能在其中执行,因为它不是为SQL定义的.
but Linq2sql is throwing an exception because SequenceEqual
cannot be executed in it because it's not defined for SQL.
我找到了此答案,它创建了一个新的Compare
方法来混淆Linq允许它.
I found this answer which creates a new Compare
method to confuse Linq into allowing it.
但是后来我发现==
也可以正常工作,因为SQL还是逐字节比较二进制文件.
But then I found that ==
works as well since SQL compares binaries byte-by-byte anyway.
但是,因为我看到了没有带来这个简单解决方案的答案,所以我想知道它是否一直有效.
But since I saw that answer which didn't bring this simple solution, I was wondering if it will really work all the time.
那么,我在这里有什么陷阱吗?
So, is there any pitfall I'm missing here?
推荐答案
正如@Sonal所说,==
将始终可用于比较byte[]
或支持相等性比较的任何其他数据类型,但是您提到的问题不会检查相等,但是对于不支持运算符的数据类型,它要求大于和小于运算符(>
和<
),因此需要一种自定义比较方法.
As @Sonal said ==
will always work for comparing byte[]
or any other data type that supports equality comparison, However the question you mention does not check equality, but it asks for greather than and less than operators (>
and <
) for a data type that does not support them, so a custom comparison method is needed.
这篇关于==会始终在SQL中对二进制文件执行SequenceEqual()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!