static void DeleteEntry(string id) { var database = new NorthwindEntities(); using (database) { if (EntryExists(database, id)) { var itemToDelete = database.Customers.Where(c => c.CustomerID == id).First(); database.Customers.Remove(itemToDelete); database.SaveChanges(); Console.WriteLine("Entry successfully deleted."); } else { Console.WriteLine("Item with the id \"{0}\" does not exist in the database.", id); } } }
static void AddEntry(string id, string name) { var database = new NorthwindEntities(); using (database) { if (EntryExists(database, id)) { Console.WriteLine("There is already an entry with the same ID"); } else { var newCustomer = new Customer() { CustomerID = id, CompanyName = name }; database.Customers.Add(newCustomer); database.SaveChanges(); Console.WriteLine("Entry successfully added."); } } }
static void ModifyEntry(string id, string newName) { var database = new NorthwindEntities(); using (database) { if (EntryExists(database, id)) { var itemToDelete = database.Customers.Where(c => c.CustomerID == id).First(); itemToDelete.CompanyName = newName; database.SaveChanges(); Console.WriteLine("Entry successfully modified."); } else { Console.WriteLine("Item with the id \"{0}\" does not exist in the database.", id); } } }
private static void FindSalesByRegionAndDate(string region, DateTime start, DateTime end) { var database = new NorthwindEntities(); using (database) { var sales = database.Orders.Where( o => o.ShipRegion == region && o.OrderDate > start && o.OrderDate < end ).Select( o => new { ShipName = o.ShipName, OrderDate = o.OrderDate }); foreach (var item in sales) { Console.WriteLine("Ship Name: {0}", item.ShipName); Console.WriteLine("Order Date: {0}", item.OrderDate); Console.WriteLine("--------------------"); } } }
private static void ShipmentsToCanada() { var database = new NorthwindEntities(); using (database) { var groupedCustomersAndOrders = database.Customers.Join( database.Orders, c => c.CustomerID, d => d.CustomerID, (c, d) => new { Name = c.CompanyName, OrderDate = d.OrderDate, ShipmentCountry = d.ShipCountry }); var items = groupedCustomersAndOrders .Where(i => i.OrderDate.Value.Year == 1997) .Where(i => i.ShipmentCountry == "Canada"); foreach (var item in items) { Console.WriteLine(item.Name); } } }
static bool EntryExists(NorthwindEntities database, string id) { return database.Customers.Where(c => c.CustomerID == id).Any(); }