title: How can the equality operator for byte arrays be faked in Linq-to-Entities while unit testing?
body: <p>There are some <code>varbinary</code> table columns in my database that i need to test for equality with a byte array, all happening through Entity Framework and Linq-to-Entities. For Linq-to-Objects, this is achieved very easily with the <code>SequenceEquals</code> extension method, however EF doesn't support it unfortunately, therefore i rely on the <code>==</code> operator which is correctly translated into a SQL query.</p>
  
  <p>E.g.</p>
  
  <pre><code>byte[] deviceId = GetDeviceId();
  
  Device device = _deviceRepository.QueryAll() //backed by a DbContext, returns IQueryable&lt;Device&gt;
  .Include(d =&gt; d.RelatedEntity1)
  .Include(d =&gt; d.RelatedEntity2)
  .Include(d =&gt; d.RelatedEntityEtc)
  .FirstOrDefault(d =&gt; d.DeviceUniqueIdentifier == deviceId &amp;&amp; ...);
  
  LoginUserOnDevice(device);
  </code></pre>
  
  <p>The problem is, though, when unit testing the piece of code involving the Linq-to-Entities query, with a fake <code>_deviceRepository</code>, for whom i use an ordinary <code>List&lt;T&gt;</code> transformed through an <code>.AsQueryable()</code> into an <code>IQueryable&lt;T&gt;</code>, the equality operator is applied with its original semantics, i.e. reference equality, so the comparison obviously fails. Of course, this doesn't happen strictly for unit testing, but in any situation where the linq query is not executed anymore against a SQL data source.</p>
  
  <p>What would be the optimal approach here? Preferably without changing the query code, so it runs correctly both when querying against a real database (using EF 6) and while unit testing. Materializing the entire collection into memory by calling <code>.ToList()</code> right after the <code>Include</code>s or anything along these lines is out of the question in my scenario.</p>