static List <Product> getProducts() { var context = new MyShopContext(); var products = context.Products.ToList(); return(products); }
static List <Category> getCategories() { var context = new MyShopContext(); var categories = context.Categories.ToList(); return(categories); }
static List <Supplier> getSuppliers() { var context = new MyShopContext(); var suppliers = context.Suppliers.ToList(); return(suppliers); }
static void Main(string[] args) { var products = getProducts(); var suppliers = getSuppliers(); var categories = getCategories(); Console.WriteLine("Products"); foreach (var item in products) { Console.WriteLine(item.ToString()); } Console.WriteLine("Suppliers"); foreach (var item in suppliers) { Console.WriteLine(item.ToString()); } Console.WriteLine("Categories"); foreach (var item in categories) { Console.WriteLine(item.ToString()); } /*● Select product with product name that begins with ‘C’*/ Console.WriteLine("Select product with product name that begins with ‘C’"); var prod = products.Where(c => c.ProductName[0] == 'C' || c.ProductName[0] == 'c'); foreach (var item in prod) { Console.WriteLine(item.ToString()); } /* ●Select product with the smallest price. */ Console.WriteLine("Select product with the smallest price."); var cheapest = products.OrderBy(c => c.Price).FirstOrDefault(); if (cheapest != null) { Console.WriteLine(cheapest.ToString()); } /*● Select cost of all products from the USA*/ Console.WriteLine("Select cost of all products from the USA"); var suppliersId = suppliers.Where(a => a.Country == "USA").Select(a => a.SupplierID.ToString()).ToArray(); var usaprices = products.Where(c => suppliersId.Contains(c.SupplierID.ToString())).Select(p => p.Price); foreach (var item in usaprices) { Console.WriteLine(item); } /*● Select suppliers that supply Condiments.*/ Console.WriteLine("Select suppliers that supply Condiments"); var categoryId = categories.Where(c => c.CategoryName == "Condiments").Select(a => a.CategoryID).FirstOrDefault(); var condimentsSupliersId = products.Where(a => a.CategoryID == categoryId).Select(p => p.SupplierID.ToString()).ToArray(); var condimentsSupliers = suppliers.Where(s => condimentsSupliersId.Contains(s.SupplierID.ToString())); foreach (var item in condimentsSupliers) { Console.WriteLine(item); } /*● Add to database new supplier with name: ‘Norske Meierier’, city: ‘Lviv’, country: ‘Ukraine’ which will supply new product with name: ‘Green tea’, price: 10, and related to category with name: ‘Beverages’.*/ Console.WriteLine("●Add to database new supplier with name: ‘Norske Meierier’, city: ‘Lviv’, country: ‘Ukraine’ which will supply new product with name: ‘Green tea’, price: 10, and related to category with name: ‘Beverages’."); var context = new MyShopContext(); Supplier newSup = context.Suppliers.Add(new Supplier { City = "Lviv", SupplierName = "Norske Meierier", Country = "Ukraine" }); categoryId = categories.Where(c => c.CategoryName == "Beverages").Select(a => a.CategoryID).FirstOrDefault(); Product newProd = context.Products.Add(new Product { CategoryID = categoryId, ProductName = "green tea", Price = 10, SupplierID = newSup.SupplierID }); context.SaveChanges(); Console.WriteLine("Suppliers"); foreach (var item in getSuppliers()) { Console.WriteLine(item.ToString()); } Console.WriteLine("Products"); foreach (var item in getProducts()) { Console.WriteLine(item.ToString()); } Console.ReadLine(); Console.WriteLine("Hello World!"); }