Both requests retrieve related data only from the first request using Lively loading (and yes. Achieved using the Include method, you guessed it), and the second request using Lazy loading , which is the default. But since your request returns only EmailAddresses due to Select() and SelectMany() , the Include() method does not change the behavior . To see when the Include() method matters in your example, read the following lines, which I will prove in one example:
To find out some difference between these two types of objects related to loading The desired loading is usually more efficient when you need the appropriate data for all the extracted rows of the primary table. And also when the relationship not too much, loaded loading will be good practice to reduce further server requests. But when you know that you don't need real estate instantly, then lazy loading might be a good choice. Also the desired download is a good choice in a situation where your db context is deleted and the lazy loading can no longer be. To prove that Lazy Loading and one is Eager Loading, consider the following code:
public List<Person> GetEmailAddresses() { using (yourEntities awlt = new yourEntities()) { var query = awlt.People .Where(p => p.LastName.Equals(lastName)); return query.ToList(); } }
After calling this method, you cannot load the associated object lazily because db is located. To prove this, follow these steps:
var query = GetEmailAddresses(); foreach (var item in query.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1)) { MessageBox.Show(item); }
And you will get this error:
The ObjectContext instance has been deleted and can no longer be used for operations that require a connection.
But if you change GetEmailAddresses to use Eager Loading as follows:
public List<Person> GetEmailAddresses() { using (yourEntities awlt = new yourEntities()) { var query = awlt.People.Include("EmailAddresses") .Where(p => p.LastName.Equals(lastName)); return query.ToList(); } }
Then the code below should work fine:
var query = GetEmailAddresses(); foreach (var item in query.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1)) { MessageBox.Show(item); }
So, in a situation where your db context will be deleted, Eager Loading will be the best choice.