Open the Program.cs
file and import the following namespaces:
using static System.Console; using Packt.CS7; using Microsoft.EntityFrameworkCore; using System.Linq;
In Program
, define a QueryingCategories
method, and add the following statements to do these:
- Create an instance of the
Northwind
class that will manage the database - Create a query for all categories that includes their related products
- Enumerate through the categories, outputting the name and number of products for each one:
static void QueryingCategories() { using (var db = new Northwind()) { WriteLine("Categories and how many products they have:"); // a query to get all categories and their related products IQueryable<Category> cats = db.Categories.Include(c => c.Products); foreach (Category c in cats) { WriteLine($"{c.CategoryName} has {c.Products.Count} products."); } } }