Categories
c# entity-framework linq sql

How to compare MD5 hashes saved as byte[] with Linq + entity framework?

I have to save a bunch of information about a large amount files in a table on SQL server 2005.

I store the full MD5 of each file as a varbinary on SQL, and .net database entity framework abstracts it as a byte[]array. Fair enough.

I am querying against said table with a query like this, comparing hashes to check if a file already is on the db:

byte[] hash = inputfile.GetFileMD5();
var query = context.CurveData
.Where(n => n.MD5.Equals(hash))
.Select(n => n.MD5)
.DefaultIfEmpty(null).FirstOrDefault();

One should never compare Arrays with .Equals() because it compares instances, not sequences. .SequenceEqual() must be used; However, the query runs perfectly fine. If I save a file and run the query against it, I indeed get the correct hash from the database; lots of other files work fine too.

Two questions:

  1. How is that possible? Is there an overload of .Equals() that checks against array equality?
  2. How can I implement the above query with .SequenceEqual()? If I try to replace .Equals() with .SequenceEqual() I get the Exception

    LINQ to Entities does not recognize the method ‘Boolean SequenceEqual[Byte](System.Collections.Generic.IEnumerable1[System.Byte], System.Collections.Generic.IEnumerable1[System.Byte])’

Thanks!