Q:

Entity Framework Generating Unnecessary Queries

Let's suppose I have three tables:
Categories, Items, And CategoriesToItems
The relationship between Categories and Items is many-to-many (through the CategoriesToItems table). 
Let's suppose that I call some method and inside of that method we execute the following code (to force query execution/enumeration of the data immediately):
var categories = context. Categories. ToList();
var items = context. Items. ToList();
var links = context. CategoriesToItems. ToList();

Immediately after this, I start examining each product like so:
foreach(Category category in categories)
{
   foreach(Item item in links. Select(l => l.Item))
   {
      //Do some stuff. 
   }
}

Why do these foreach loops generate queries to the database?  Shouldn't EF already know all of the required information because I loaded the categories, items, and the link table entries? 
Interestingly, if I change things up a bit and load the data like so
var items = context. Items. ToList();
var categories = context. Categories. Include("CategoriesToItems").ToList();

Running through a similar loop doesn't generate queries when I want to know what products are in any given category. 
The reason I care is that loading the data the first way is much faster, but I have to perform operations on the data immediately afterwards and the performance of that is much slower (because of the additional queries generated with each pass).  
The actual include clauses are more complicated, as we need data from several different tables which is why it's faster to load the whole tables individually than to load the whole tables through an elaborately generated multiple join query (we need all of the data that's in them).  In the real application there are more tables involved (categories can have tags, items can have tags, both relationships are many-to-many--I'm sure you get the picture--the "do stuff" section is actually building a dictionary of items/categories to the tags they have and tags are inherited, so a category tagged with X means all of its products are tagged with X). 

To generalize/simplify this a little bit: 
Why does the Entity Framework generate queries about relationships when I've loaded every single row from all of the involved tables?  Is there anything I can do to help this situation? 

Edit: I should add that I'm assuming it's actually making a call to the database because of the ADO.NET query entries that show up in LiveTrace when those specific lines of code execute and because the performance is noticeably different.  So if I'm misunderstanding what those LiveTrace entries mean, then I guess I've got a different problem all together. 

A:

Try to turn off lazy loading prior to executing any query:
context. ContextOptions. LazyLoadingEnabled = false;

