title: Why does EF throw "NotSupportedException: The method 'First' can only be used as a final query operation"
body: <pre><code>int[] ids1 = { 1, 2, 3 };
  int[] ids2 = { 1, 5, 6 };
  
  var result = from a in ids1
               where a == ids2.First()
               select a;  
  foreach (var item in result) ; //ok
  
  
  var employees = from c in context.Employees.
                  where c.EmployeeID == ids1.First() 
                  select c;   
  foreach (var item in employees); // NotSupportedException
  </code></pre>
  
  <p>When trying to call <code>ids1.First</code> within Linq-to-Entities query, I get an exception
  <em>System.NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead</em>.</p>
  
  <p>a) I don't understand why <code>First</code> can only be used as a final query operation, since in our example <code>First</code> is called on <code>IEnumerable&lt;&gt;</code> ( <code>ids1.First()</code> ) and not on <code>IQueryable&lt;&gt;</code>. In other words, <code>First</code> is called within Linq-to-Objects query and not within Linq-to-Entities query?! </p>
  
  <p>b) Anyways, why must <code>First</code> be used as a final query operation, while <code>FirstOrDefault</code> doesn't have to be final query operation?</p>
  
  <p>Thank you</p>
  
  <p><br /></p>
  
  <p>REPLY:</p>
  
  <blockquote>
    <p>As for the difference between First() and FirstOrDefault() - I don't
    know. Have you tried it, and does it work?</p>
  </blockquote>
  
  <p>Yes it works</p>
  
  <blockquote>
    <p>No, First() is being called in the LINQ to Entities query. Your where
    clause will be converted to: Where(c => c.EmployeeID == ids1.First())</p>
  </blockquote>
  
  <p>a) I'm a bit confused now. I realize <code>ids1.First</code> is essentially called within Linq-to-Entities query, but the fact remains that <code>First</code> is called on <code>IEnumerable&lt;&gt;</code>, and as such <code>First</code> is called within Linq-to-Objects query, and this Linq-to-Object query is in turn called within Linq-To-entities query – at least that's how I understand it?! </p>
  
  <p>Or are you implying that <code>First</code> is somehow called on <code>IQeryable&lt;&gt;</code>?</p>
  
  <p>b) I realize that <code>(c =&gt; c.EmployeeID == ids1.First())</code> will get converted into an expression tree, but why isn't <code>ids1.First()</code> executed before the conversion happens? </p>
  
  <p>c) Anyways, once the conversion into expression tree does happen, I assume when sql provider receives the expression tree of our <code>where</code> clause and tries to convert it into Sql command, this Sql provider doesn't have "the power" to execute <code>ids1.First</code> in order to get the result back ( which it would then put into Sql query  ), and hence the exception?!</p>
  
  <p><BR /></p>
  
  <p><strong>SECOND REPLY:</strong></p>
  
  <p>a) I'm still confused why <code>ids1.First</code> isn't executed before the conversion into expression tree happens?! Namelly, with the following  clause  </p>
  
  <pre><code>Where(c =&gt; c.EmployeeID == 2+3) 
  </code></pre>
  
  <p>the expression <code>2+3</code> gets executed before this <em>Where</em> clause is transformed into expression tree! And <code>ids.First</code> is also an expression of sorts, so I would expect similar behavior?!</p>
  
  <p>b) Sorry for being repetitive, but it's really bugging me whether my assumption - that
  <code>First</code> is called within Linq-to-Objects query, and this Linq-to-Object query is in turn called within Linq-To-entities query – is correct?!</p>
  
  <p>c) Perhaps I misunderstood your post, but are you implying that most other Linq-to-Object operators can be called on <code>IEnumerable&lt;&gt; E</code>,  even if <code>E</code> is contained within Linq-to-Entities query ?</p>