title: Why does Entity Framework ignore order by when followed with distinct?
body: <p>I'd like to know why the following join returns values in an unordered fashion. Does distinct imply that the order of the <code>IQueryable</code> passed is not guaranteed, therefore EF does not bother generating an order by clause in the SQL. </p>
  
  <pre><code> var currentUtc = DateTime.Now;
   var data = (from a in ItemsA
       join c in Customers on a.Fk_CustomerID equals c.ID into customerSubset
       from cs in customerSubset.DefaultIfEmpty()
       where cs.Age &gt; 18)                                    
       orderby a.ID ascending
       select new
       {
         a.ID,
         a.someProperty,
         cs.CustomerUID,
         CustomerName = cs.Name,                               
         UpdateUTC = currentUtc
       }).Distinct().Take(1000).ToList();
  </code></pre>
  
  <p>The strange thing is that removing the distinct adds the order by clause in the inner query of the generated SQL (you can use Linqpad to see the SQL generated for eg.)</p>
  
  <p>Now if I replace the last line with</p>
  
  <pre><code>.Distinct().OrderBy(x =&gt; s.ID).Take(1000).ToList();
  </code></pre>
  
  <p>I'm getting the order by clause in the SQL, whether or not I have the <code>orderby a.ID ascending</code> in the inner query. This makes sense, but why is chaining distinct after orderby also not yield the same order?</p>