title: How to insert a method inside a LINQ lambda extension method
body: <p>I have to place a method inside a LINQ lambda. I have the following code:</p>
  
  <pre><code>string productName = "Some product";
  
  searchShoppingCart = shoppingCart.Where(m =&gt; productName.Equals(_ProductRepository.GetProductName(m.Id)));
  </code></pre>
  
  <p>Basically this code is used to select all the <code>shoppingCart</code> instances that contain <code>productName</code>. </p>
  
  <p>The method <code>string GetProductName(int shoppingCartId)</code> is a method  from <code>_ProductRepository</code> that returns the name of one product. How this method is implemented and its logic does not matter.</p>
  
  <p>The problem is that LINQ throws an <strong>exception</strong>. I know the reasons why this exception is thrown, I just would like to know a workaround. I tried </p>
  
  <pre><code>var productContained = shoppingCart.Select(sc =&gt; new
                      {
                          scId = sc.Id,
                          productName = _ProductRepository.GetProductName(sc.Id)
                      });
  
  searchShoppingCart = shoppingCart.Where(sc =&gt; sc.Id.Equals(productContained.Where(pc =&gt; pc.productName.Equals(productName))
                                                                             .Select(pc =&gt; pc.Id)));
  </code></pre>
  
  <p>but it gives me the same exception. Is there any other workaround?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The exception thrown is </p>
  
  <p><em>LINQ to Entities does not recognize the method 'System.String GetProductName(Int32)' method, and this method cannot be translated into a store expression.</em></p>