title: Entity Framework Core Random Items keep getting fetched Inside IEnumerable
body: <p>I have the following code in C#, .Net Core 2.2, Entity Framework Core 2.2, MS SQL Server 2017:</p>
  
  <pre><code>IEnumerable&lt;PostData&gt; topicsData = context.Topics
                      .OrderBy(t =&gt; Guid.NewGuid())
                      .Take(3)
                      .Select(t =&gt; new PostData()
                      {
                          TopicID = t.TopicID,
                          UserID = t.UserID,
                          User = t.User,
                          MessageID = t.MessageID,
                          Message = t.Message,
                          Replies = t.Replies
                      })
                      .AsNoTracking()
                      .AsEnumerable();
  
  List&lt;PostModel&gt; posts = new List&lt;PostModel&gt;();
  List&lt;UserModel&gt; users = new List&lt;UserModel&gt;();
  
  foreach (var topic in topicsData)
  {
      var postModel = new PostModel()
              {
                  id = topic.TopicID,
                  userId = topic.UserID,
                  message = topic.Message.Content,
                  replies = topic.Replies,
              };
      posts.Add(postModel);
  
      var userModel = new UserModel()
              {
                  id = topic.User.UserID,
                  username = topic.User.Username
              };
  
      if (!users.Any(u =&gt; u.id == userModel.id)) 
      {
          users.Add(userModel);
      }
  }
  </code></pre>
  
  <p>What I basically want to do is fetch a set of 3 random items from Topics table in my db, and map the result into 2 separate lists, one for for the topics themselves (posts) and one for distinct users.</p>
  
  <p>As you see I am taking the 3 random items using the commands: </p>
  
  <pre><code>.OrderBy(t =&gt; Guid.NewGuid()).Take(3)
  </code></pre>
  
  <p>However, I have a problem: In the first Select where I populate the topicsData object, I get a list of items, and after the foreach loop finishes to run I see a whole other list of items.</p>
  
  <p>When I debug this code, every single line in my foreach loop causes the data in topicsData to be fetched again with a new random set of 3 Topic items.</p>
  
  <p>I don't understand what's wrong. When I used <code>AsEnumerable()</code> on my <code>topicsData</code> object didn't I get an entirely new separate list of items and stopped working against my DB?</p>
  
  <p>Why does it keep going to the DB and fetching more random items?</p>
  
  <p>And how can I make it stop re-fetching and get fixed on the list of items that it initially fetched?</p>