title: How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?
body: <p>I have the following LINQ query executed using Entity Framework (v6.1.1):</p>
  
  <pre><code>private IList&lt;Customer&gt; GetFullCustomers(IEnumerable&lt;int&gt; customersIds)
  {
      IQueryable&lt;Customer&gt; fullCustomerQuery = GetFullQuery();
      return fullCustomerQuery.Where(c =&gt; customersIds.Contains(c.Id)).ToList();
  }
  </code></pre>
  
  <p>This query is translated into fairly nice SQL:</p>
  
  <pre><code>SELECT 
  [Extent1].[Id] AS [Id], 
  [Extent1].[FirstName] AS [FirstName]
  -- ...
  FROM [dbo].[Customer] AS [Extent1]
  WHERE [Extent1].[Id] IN (1, 2, 3, 5)
  </code></pre>
  
  <p>However, I get a very significant performance hit on a query compilation phase. Calling:</p>
  
  <pre><code>ELinqQueryState.GetExecutionPlan(MergeOption? forMergeOption) 
  </code></pre>
  
  <p>Takes ~50% of the time of <strong>each request</strong>. Digging deeper, it turned out that query gets re-compiled every time I pass different <em>customersIds</em>.
  According to <a href="http://msdn.microsoft.com/en-us/data/hh949853.aspx#4">MSDN article</a>, this is an expected behavior because IEnumerable that is used in a query is considered volatile and is part of SQL that is cached. That's why SQL is different for every different combination of <em>customersIds</em> and it always has different hash that is used to get compiled query from cache.</p>
  
  <p>Now the question is: <strong>How can I avoid this re-compilation while still querying with multiple <em>customersIds</em>?</strong></p>