Database Reference
In-Depth Information
Console.WriteLine("\nAll products, including those without ratings
");
foreach (var product in products)
{
if (product.Rating != 0)
Console.WriteLine("\t{0} [rating: {1}]", product.Name,
product.Rating.ToString());
}
}
using (var context = new EFRecipesEntities())
{
var esql = @"select value p from products as p
order by case when p.TopSelling is null then 0
else p.TopSelling.Rating end desc";
var products =((IObjectContextAdapter)context).ObjectContext.CreateQuery<Product>(esql);
Console.WriteLine("\nAll products, including those without ratings
");
foreach (var product in products)
{
Console.WriteLine("\t{0} [rating: {1}]", product.Name,
product.TopSelling == null ? "0"
: product.TopSelling.Rating.ToString());
}
}
Following is the output of the code in Listing 3-22:
Top selling products sorted by rating
Prairie Home Dutch Oven [rating: 4]
Green River Tent [rating: 3]
QuickFire Fire Starter [rating: 2]
Trailrunner Backpack [rating: 0]Top selling products sorted by rating
Prairie Home Dutch Oven [rating: 4]
Green River Tent [rating: 3]
QuickFire Fire Starter [rating: 2]
Trailrunner Backpack [rating: 0]Top selling products sorted by rating
Prairie Home Dutch Oven [rating: 4]
Green River Tent [rating: 3]
QuickFire Fire Starter [rating: 2]
Trailrunner Backpack [rating: 0]
How It Works
In Listing 3-22, we show three slightly different approaches to this problem. The first approach is the simplest, as
Entity Framework handles the join automatically for related entities based on a navigation property that was created
between the two entities when the model was created. The entities are in a one-to-zero or one association, which
means that Entity Framework will automatically generate a SQL query that includes a left-outer join between the two
entities. When the product entities are materialized, any associated top sellers are also materialized. The TopSeller
 
Search WWH ::




Custom Search