title: Entity Framework Include OrderBy random generates duplicate data
body: <p>When I retrieve a list of items from a database including some children (via .Include), and order the randomly, EF gives me an unexpected result.. I creates/clones addition items..</p>
  
  <p>To explain myself better, I've created a small and simple EF CodeFirst project to reproduce the problem.
  First i shall give you the code for this project.</p>
  
  <h2>The project</h2>
  
  <p>Create a basic MVC3 project and add the EntityFramework.SqlServerCompact package via Nuget.<br>
  That adds the latest versions of the following packages:</p>
  
  <ul>
  <li>EntityFramework v4.3.0</li>
  <li>SqlServerCompact v4.0.8482.1</li>
  <li>EntityFramework.SqlServerCompact v4.1.8482.2</li>
  <li>WebActivator v1.5</li>
  </ul>
  
  <p><strong>The Models and DbContext</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections.Generic;
  using System.Data.Entity;
  
  namespace RandomWithInclude.Models
  {
      public class PeopleContext : DbContext
      {
          public DbSet&lt;Person&gt; Persons { get; set; }
          public DbSet&lt;Address&gt; Addresses { get; set; }
      }
  
      public class Person
      {
          public int ID { get; set; }
          public string Name { get; set; }
  
          public virtual ICollection&lt;Address&gt; Addresses { get; set; }
      }
  
      public class Address
      {
          public int ID { get; set; }
          public string AdressLine { get; set; }
  
          public virtual Person Person { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>The DB Setup and Seed data: EF.SqlServerCompact.cs</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections.Generic;
  using System.Data.Entity;
  using System.Data.Entity.Infrastructure;
  using RandomWithInclude.Models;
  
  [assembly: WebActivator.PreApplicationStartMethod(typeof(RandomWithInclude.App_Start.EF), "Start")]
  
  namespace RandomWithInclude.App_Start
  {
      public static class EF
      {
          public static void Start()
          {
              Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
              Database.SetInitializer(new DbInitializer());
          }
      }
      public class DbInitializer : DropCreateDatabaseAlways&lt;PeopleContext&gt;
      {
          protected override void Seed(PeopleContext context)
          {
              var address1 = new Address {AdressLine = "Street 1, City 1"};
              var address2 = new Address {AdressLine = "Street 2, City 2"};
              var address3 = new Address {AdressLine = "Street 3, City 3"};
              var address4 = new Address {AdressLine = "Street 4, City 4"};
              var address5 = new Address {AdressLine = "Street 5, City 5"};
              context.Addresses.Add(address1);
              context.Addresses.Add(address2);
              context.Addresses.Add(address3);
              context.Addresses.Add(address4);
              context.Addresses.Add(address5);
              var person1 = new Person {Name = "Person 1", Addresses = new List&lt;Address&gt; {address1, address2}};
              var person2 = new Person {Name = "Person 2", Addresses = new List&lt;Address&gt; {address3}};
              var person3 = new Person {Name = "Person 3", Addresses = new List&lt;Address&gt; {address4, address5}};
              context.Persons.Add(person1);
              context.Persons.Add(person2);
              context.Persons.Add(person3);
          }
      }
  }
  </code></pre>
  
  <p><strong>The controller: HomeController.cs</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Data.Entity;
  using System.Linq;
  using System.Web.Mvc;
  using RandomWithInclude.Models;
  
  namespace RandomWithInclude.Controllers
  {
      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              var db = new PeopleContext();
              var persons = db.Persons
                                  .Include(p =&gt; p.Addresses)
                                  .OrderBy(p =&gt; Guid.NewGuid());
  
              return View(persons.ToList());
          }
      }
  }
  </code></pre>
  
  <p><strong>The View: Index.cshtml</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>@using RandomWithInclude.Models
  @model IList&lt;Person&gt;
  
  &lt;ul&gt;
      @foreach (var person in Model)
      {
          &lt;li&gt;
              @person.Name
          &lt;/li&gt;
      }
  &lt;/ul&gt;
  </code></pre>
  
  <p>this should be all, and you application should compile :)</p>
  
  <hr>
  
  <h2>The problem</h2>
  
  <p>As you can see, we have 2 straightforward models (Person and Address) and Person can have multiple Addresses.<br>
  We seed the generated database <strong>3 persons</strong> and 5 addresses.<br>
  If we get all the persons from the database, including the addresses and randomize the results and just print out the names of those persons, <strong>that's where it all goes wrong.</strong>  </p>
  
  <p>As a result, i sometimes get 4 persons, sometimes 5 and sometimes 3, and i expect 3. Always.<br>
  e.g.:</p>
  
  <ul>
  <li>Person 1 </li>
  <li>Person 3 </li>
  <li>Person 1 </li>
  <li>Person 3 </li>
  <li>Person 2 </li>
  </ul>
  
  <p>So.. it's copying/cloning data! And that's not cool..<br>
  It just seems that EF looses track of what addresses are a child of which person..  </p>
  
  <p>The generated SQL query is this:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT 
      [Project1].[ID] AS [ID], 
      [Project1].[Name] AS [Name], 
      [Project1].[C2] AS [C1], 
      [Project1].[ID1] AS [ID1], 
      [Project1].[AdressLine] AS [AdressLine], 
      [Project1].[Person_ID] AS [Person_ID]
  FROM ( SELECT 
      NEWID() AS [C1], 
      [Extent1].[ID] AS [ID], 
      [Extent1].[Name] AS [Name], 
      [Extent2].[ID] AS [ID1], 
      [Extent2].[AdressLine] AS [AdressLine], 
      [Extent2].[Person_ID] AS [Person_ID], 
      CASE WHEN ([Extent2].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
      FROM  [People] AS [Extent1]
      LEFT OUTER JOIN [Addresses] AS [Extent2] ON [Extent1].[ID] = [Extent2].[Person_ID]
  )  AS [Project1]
  ORDER BY [Project1].[C1] ASC, [Project1].[ID] ASC, [Project1].[C2] ASC
  </code></pre>
  
  <h2>Workarounds</h2>
  
  <ol>
  <li>If i remove the <code>.Include(p =&gt;p.Addresses)</code> from the query, everything goes fine. but of course the addresses aren't loaded and accessing that collection will make a new call to the database every time.</li>
  <li>I can first get the data from the database and randomize later by just adding a .ToList() before the .OrderBy.. like this: <code>var persons = db.Persons.Include(p =&gt; p.Addresses).ToList().OrderBy(p =&gt; Guid.NewGuid());</code></li>
  </ol>
  
  <p>Does anybody have any idea of why it is happening like this?<br>
  Might this be a bug in the SQL generation?</p>