title: Using an IEqualityComparer with a LINQ to Entities Except clause
body: <p>I have an entity that I'd like to compare with a subset and determine to select all except the subset.</p>
  
  <p>So, my query looks like this:</p>
  
  <pre><code>Products.Except(ProductsToRemove(), new ProductComparer())
  </code></pre>
  
  <p>The <code>ProductsToRemove()</code> method returns a <code>List&lt;Product&gt;</code> after it performs a few tasks. So in it's simplest form it's the above.</p>
  
  <p>The <code>ProductComparer()</code> class looks like this:</p>
  
  <pre><code>public class ProductComparer : IEqualityComparer&lt;Product&gt;
  {
      public bool Equals(Product a, Product b)
      {
          if (ReferenceEquals(a, b)) return true;
  
          if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
              return false;
  
          return a.Id == b.Id;
      }
  
      public int GetHashCode(Product product)
      {
          if (ReferenceEquals(product, null)) return 0;
          var hashProductId = product.Id.GetHashCode();
          return hashProductId;
      }
  }
  </code></pre>
  
  <p>However, I continually receive the following exception:</p>
  
  <blockquote>
    <p>LINQ to Entities does not recognize
    the method
    'System.Linq.IQueryable<code>1[UnitedOne.Data.Sql.Product]
    Except[Product](System.Linq.IQueryable</code>1[UnitedOne.Data.Sql.Product],
    System.Collections.Generic.IEnumerable<code>1[UnitedOne.Data.Sql.Product],
    System.Collections.Generic.IEqualityComparer</code>1[UnitedOne.Data.Sql.Product])'
    method, and this method cannot be
    translated into a store expression.</p>
  </blockquote>