title: Linq2SQL "Local sequence cannot be used in LINQ to SQL" error
body: <p>I have a piece of code which combines an in-memory list with some data held in a database. This works just fine in my unit tests (using a mocked Linq2SqlRepository which uses List).</p>
  
  <pre><code>    public IRepository&lt;OrderItem&gt; orderItems { get; set; }
  
      private List&lt;OrderHeld&gt; _releasedOrders = null;
      private List&lt;OrderHeld&gt; releasedOrders
      {
          get
          {
              if (_releasedOrders == null)
              {
                  _releasedOrders = new List&lt;nOrderHeld&gt;();
              }
              return _releasedOrders;
          }
      }
  
      .....
  
      public int GetReleasedCount(OrderItem orderItem)
      {
          int? total =
              (
                  from item in orderItems.All
                  join releasedOrder in releasedOrders
                      on item.OrderID equals releasedOrder.OrderID
                  where item.ProductID == orderItem.ProductID
                  select new
                  {
                      item.Quantity,
                  }
  
              ).Sum(x =&gt; (int?)x.Quantity);
  
          return total.HasValue ? total.Value : 0;
      }
  </code></pre>
  
  <p>I am getting an error I don't really understand when I run it against a database.</p>
  
  <blockquote>
    <p>Exception information:<br/>
    &nbsp;&nbsp;&nbsp;&nbsp;Exception type: System.NotSupportedException<br/>
    &nbsp;&nbsp;&nbsp;&nbsp;Exception message: Local sequence cannot be used in LINQ to SQL
    implementation of query operators
    except the Contains() operator.</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p><em>I'm guessing it's to do with the fact that <strong>orderItems</strong> is on the database and <strong>releasedItems</strong> is in memory.</em></p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I have changed my code based on the answers given (thanks all)</p>
  
  <pre><code>    public int GetReleasedCount(OrderItem orderItem)
      {
          var releasedOrderIDs = releasedOrders.Select(x =&gt; x.OrderID);
  
          int? total =
              (
                  from item in orderItems.All
                  where releasedOrderIDs.Contains(item.OrderID)
                     &amp;&amp; item.ProductID == orderItem.ProductID
                  select new
                  {
                      item.Quantity,
                  }
  
              ).Sum(x =&gt; (int?)x.Quantity);
  
          return total.HasValue ? total.Value : 0;
      }
  </code></pre>