private static void getProductsNumberForCategory(ProdContext prodContext) { var query = from c in prodContext.Categories select new { CategoryID = c.CategoryID, CategoryName = c.CategoryName, ProductsNumber = c.Products.Count() }; foreach (var category in query) { Console.WriteLine("{0}\t{1}\t{2}", category.CategoryID, category.CategoryName, category.ProductsNumber); } }
private static void getProductsEagerLoading(ProdContext prodConext) { var query = prodConext.Categories .Include(c => c.Products) .ToList(); foreach (var category in query) { Console.Write("{0}\t{1}\n", category.CategoryID, category.CategoryName); foreach (var product in category.Products) { Console.WriteLine("\t{0}\t{1}", product.ProductID, product.Name); } } }
private static void getCategoriesAndProductsJoin(ProdContext prodContext) { var query = from c in prodContext.Categories join p in prodContext.Products on c.CategoryID equals p.CategoryID select new { CategoryID = c.CategoryID, CategoryName = c.CategoryName, ProductID = p.ProductID, ProductName = p.Name }; foreach (var product in query) { Console.WriteLine("{0}\t{1}\t{2}\t{3}", product.CategoryID, product.CategoryName, product.ProductID, product.ProductName); } }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); _context = new ProdContext(); }