title: .netcore EF linq - this is a BUG? Very strange behavior
body: <p><a href="https://i.stack.imgur.com/Gsb1E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gsb1E.png" alt="enter image description here"></a>I have two table in sql. Document and User. Document have relation to User and I want to get users that I sent document recently.</p>
  
  <p>I need to sort by the date document was sent and get unique (distinct) user with relation to this document</p>
  
  <p>This is my linq queries</p>
  
  <pre><code>var recentClients = documentCaseRepository.Entities
              .Where(docCase =&gt; docCase.AssignedByAgentId == WC.UserContext.UserId)
              .OrderByDescending(userWithDate =&gt; userWithDate.LastUpdateDate)
              .Take(1000) // I need this because if I comment this line then EF generate completely different sql query. 
              .Select(doc =&gt; new { doc.AssignedToClient.Id, doc.AssignedToClient.FirstName, doc.AssignedToClient.LastName })                
              .Distinct()
              .Take(configuration.MaxRecentClientsResults)                
              .ToList();
  </code></pre>
  
  <p>and generated sql query is: </p>
  
  <pre><code>SELECT DISTINCT TOP(5) [t].*
  FROM (
       SELECT TOP(1000) [docCase.AssignedToClient].[Id]
       FROM [DocumentCase] AS [docCase]
       INNER JOIN [User] AS [docCase.AssignedToClient] 
         ON ([docCase].[AssignedToClientId] = [docCase.AssignedToClient].[Id])
       WHERE [docCase].[AssignedByAgentId] = 3
       ORDER BY [docCase].[LastUpdateDate] DESC
  ) 
  AS [t]
  </code></pre>
  
  <p>Every thing is correct for now. But if I delete this line</p>
  
  <pre><code>.Take(1000) // I need this because...
  </code></pre>
  
  <p>EF generated completely different query such as: </p>
  
  <pre><code>SELECT DISTINCT TOP(5) 
      [docCase.AssignedToClient].[Id]
  FROM [DocumentCase] AS [docCase]
  INNER JOIN [User] AS [docCase.AssignedToClient] 
      ON ([docCase].[AssignedToClientId] = [docCase.AssignedToClient].[Id])
  WHERE [docCase].[AssignedByAgentId] = 3
  </code></pre>
  
  <p>My question is: why EF not generated orderby clause and subquery with distinct? 
  This is a BUG EF or I'm doing something wrong? And what I must do to generate in linq this sql query ()</p>
  
  <pre><code>SELECT DISTINCT TOP 5 [t].*
  FROM ( SELECT [docCase.AssignedToClient].[Id]
   FROM [DocumentCase] AS [docCase]
   INNER JOIN [User] AS [docCase.AssignedToClient] 
     ON [docCase].[AssignedToClientId] = [docCase.AssignedToClient].[Id]
   WHERE [docCase].[AssignedByAgentId] = 1
   ORDER BY [docCase].[LastUpdateDate] DESC
  ) AS [t]
  </code></pre>