diff --git a/Chapter10/WorkingWithEFCore/WorkingWithEFCore/Program.cs b/Chapter10/WorkingWithEFCore/WorkingWithEFCore/Program.cs index 25dcbcc..ff3ad4a 100644 --- a/Chapter10/WorkingWithEFCore/WorkingWithEFCore/Program.cs +++ b/Chapter10/WorkingWithEFCore/WorkingWithEFCore/Program.cs @@ -1,3 +1,26 @@ -using Packt.Shared; +using Microsoft.EntityFrameworkCore; +using Packt.Shared; -Console.WriteLine($"Using {ProjectConstants.DatabaseProvider} database provider."); \ No newline at end of file +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? 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"); + } + } +} \ No newline at end of file