title: LINQ generating sub query for a simple order by
body: <p>I'm having trouble understanding why the SQL output has a sub-query for a simple query I wrote in LINQ. This is my code:</p>
  
  <pre><code>var list = db.User.Where(u =&gt; u.Name == somename).OrderBy(u =&gt; u.IdUser).ToList();
  </code></pre>
  
  <p>where <em>somename</em> is a parameter I'm passing at execution time.</p>
  
  <p>The output SQL is:</p>
  
  <pre><code>SELECT
  Project1.IdUser, 
  Project1.Name
  FROM (SELECT
  Extent1.IdUser, 
  Extent1.Name
  FROM user AS Extent1
  WHERE Extent1.Name = 'John' /* @p__linq__0 */) AS Project1
  ORDER BY 
  Project1.IdUser ASC
  </code></pre>
  
  <p>Should the output really have a sub-query for something that simple?</p>
  
  <p>I also tried</p>
  
  <pre><code>var list = db.User.Where(u =&gt; u.Name.Equals(somename)).OrderBy(u =&gt; u.IdUser).ToList();
  </code></pre>
  
  <p>which generates the same output as above.</p>
  
  <p>If I hard code the parameter, like:</p>
  
  <pre><code>var list = db.User.Where(u =&gt; u.Name == "John").OrderBy(u =&gt; u.IdUser).ToList();
  </code></pre>
  
  <p>It works as expected, generating only</p>
  
  <pre><code>SELECT
  Extent1.IdUser, 
  Extent1.Name
  FROM user AS Extent1
  WHERE 'John' /* @gp1 */ = Extent1.Name
  ORDER BY 
  Extent1.IdUser ASC
  </code></pre>
  
  <p>A few things I'm using:</p>
  
  <ul>
  <li>EntityFramework 5, .NET 4.5</li>
  <li>SQL Server 2012</li>
  <li>Glimpse (which uses MiniProfiler) to see the SQL generated</li>
  </ul>
  
  <p>I'm not a LINQ expert, so what am I missing here?</p>