title: iterating over DbSet<TEntity> vs IQueryable<out T>
body: <p>Using Entity Framework I select some data from a table and iterate over it with <code>foreach</code> loop. I am wondering when is the data getting queried in the following examples?</p>
  
  <p>Example 1.</p>
  
  <pre><code>var countries = db.WorldCountries;
  foreach(var item in countries)
  {
      Console.WriteLine("Country: {0}", item.Country);
  }
  </code></pre>
  
  <p>Example 2.</p>
  
  <pre><code>var countries = db.WorldCountries.Where(t =&gt; t.Country == "Country Name");
  foreach(var item in countries)
  {
      Console.WriteLine("Country: {0}", item.Country);
  }
  </code></pre>
  
  <p>In the first example, <code>countries</code> is <code>DbSet&lt;WorldCountries&gt;</code> In the second example, <code>countries</code> is <code>IQueryable&lt;out WorldCountries&gt;</code>.</p>
  
  <p>Without the <code>.ToList()</code> in the above examples how is the data retrieved? Is the entire data set retrieved when the <code>foreach</code> loop starts (as if <code>.ToList()</code> were called at the beginning of the first iteration) or queries are issued to the database on each iteration of the loop. </p>
  
  <p>Thanks.</p>