title: How to invoke Expression<Func<Entity, bool>> against a collection
body: <p>I have an interface that defines a repository from the Repository pattern:</p>
  
  <pre><code>interface IRepository
  {
      List&lt;Customer&gt; GetAllCustomers(Expression&lt;Func&lt;Customer, bool&gt;&gt; expression);
  }
  </code></pre>
  
  <p>I've implemented it against Entity Framework:</p>
  
  <pre><code>class EntityFrameworkRepository
  {
      public List&lt;Customer&gt; GetAllCustomers(Expression&lt;Func&lt;Customer, bool&gt;&gt; expression)
      {
          return DBContext.Customers.Where(expression).ToList();
      }
  }
  </code></pre>
  
  <p>That seems to work well, it allows me to do something like:</p>
  
  <pre><code>var customers = entityFrameworkRepository.Where(
      customer =&gt; String.IsNullOrEmpty(customer.PhoneNumber)
  );
  </code></pre>
  
  <p>Now I'd like to have an InMemoryRepository for testing and demo purposes. I attempted to create one:</p>
  
  <pre><code>class InMemoryRepository
  {
      Dictionary&lt;int, Customer&gt; Customers {get; set;} = new Dictionary&lt;int, Customer&gt;();
  
      public List&lt;Customer&gt; GetAllCustomers(Expression&lt;Func&lt;Customer, bool&gt;&gt; expression)
      {
          //what do I put here?
      }
  }
  </code></pre>
  
  <p>As you can see in the above code, I'm stumped on what to do for <code>InMemoryRepository.GetAllCustomers</code> implementation. What should I do there to filter the Customers by the provided expression and return the results?</p>
  
  <p>I tried:</p>
  
  <pre><code>return Customers.Where(expression));
  </code></pre>
  
  <p>But obviously it's expecting a <code>Func&lt;KeyValuePair&lt;int, Customer&gt;, bool&gt;</code> so I get a compilation error:</p>
  
  <blockquote>
    <p>Error CS1929  'Dictionary' does not contain a definition for 'Where' and the best extension method overload 'Queryable.Where(IQueryable, Expression>)' requires a receiver of type 'IQueryable' DataAccess.InMemory</p>
  </blockquote>