Will == always perform a SequenceEqual() on binary

2019-08-25 06:51发布

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[].

but Linq2sql is throwing an exception because SequenceEqual cannot be executed in it because it's not defined for SQL.

I found this answer which creates a new Compare method to confuse Linq into allowing it.

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?

1条回答
SAY GOODBYE
2楼-- · 2019-08-25 07:11

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.

查看更多
登录 后发表回答