10.4 Querying EF Core models -> use LINQ to query all categories and their products
This commit is contained in:
parent
b851fd937a
commit
d9e8bf8553
@ -1,3 +1,26 @@
|
||||
using Packt.Shared;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Packt.Shared;
|
||||
|
||||
Console.WriteLine($"Using {ProjectConstants.DatabaseProvider} database provider.");
|
||||
Console.WriteLine($"Using {ProjectConstants.DatabaseProvider} database provider.");
|
||||
QueryingCategories();
|
||||
|
||||
static void QueryingCategories()
|
||||
{
|
||||
using (Northwind db = new())
|
||||
{
|
||||
Console.WriteLine("Categories and how many products they have:");
|
||||
// a query to get all categories and their related products
|
||||
IQueryable<Category>? categories = db.Categories?
|
||||
.Include(c => c.Products);
|
||||
if (categories is null)
|
||||
{
|
||||
Console.WriteLine("No categories found");
|
||||
return;
|
||||
}
|
||||
// execute query and enumerate results
|
||||
foreach (var c in categories)
|
||||
{
|
||||
Console.WriteLine($"{c.CategoryName} has {c.Products.Count} products");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user