0

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?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • == should perform with Sql. Check out this link https://stackoverflow.com/questions/26345381/comparing-byte-in-linq-query – Sonal Borkar Dec 03 '18 at 19:06

1 Answers1

1

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.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Thanks. Upvoted. But I'll wait because the first to answer it was actually the [answerer in the link](https://stackoverflow.com/questions/42502643/how-to-compare-two-byte-arrays-with-greater-than-or-less-than-operator-value-in/42502969#comment94062884_42502969) so I'll see if they post their answer here. – ispiro Dec 03 '18 at 20:10