If you only want to load the entities that are related to one primary entity, you can use the Load()
extension method.
You can call the Load()
method anywhere in your code. It doesn’t need to be in the context of a query definition:
MyRecipe.Ingredients.Load()
Master-detail is a very common scenario in data-bound applications. You present the user with a list of objects, recipes or orders, and when they select one, the application displays the details of that object. Load()
is perfect for that situation.
Put On Your Thinking Hat
Can you rewrite that ugly snippet from the last Thinking Hat that made 6 round trips to the database so that it makes only one?
Put On Your Thinking Hat
How’d you do?
var q = (from r in context.Recipes.Include("Ingredients")
orderby r.RecipeName
select r).Take(5);
foreach (Recipe r in q)
{
String str;
str = r.RecipeName + " has " + r.Ingredients.Count() + " ingredients";
Console.Writeline(str);
}
Dim q = (From r In context.Recipes.Include("Ingredients")
Order By r.RecipeName
Select r).Take(5);
For Each (r As Recipe in q)
Dim str As String
str = r.RecipeName + " has " + r.Ingredients.Count() + " ingredients"
Console.Writeline(str)
Next r