static void CreateNewProduct(NorthwindEntities northwindEntities, string productName, bool discontinued) { Product newProduct = new Product{ ProductName = productName, Discontinued = discontinued }; northwindEntities.Products.AddObject(newProduct); Update(northwindEntities); }
static void JoinCustomerWithSupplierExtentedMethods(NorthwindEntities northwindEntities) { var customerSupplier = northwindEntities.Customers. Join(northwindEntities.Suppliers, (customer=>customer.Country), (supplier=>supplier.Country), (customer,supplier)=> new { CustomerName = customer.CompanyName, Supplier = supplier.CompanyName, Country = customer.Country }); Logger.PrintQueries(customerSupplier); }
static void JoinCustomerWithSupplier(NorthwindEntities northwindEntities) { var customerSupplier = from customer in northwindEntities.Customers join supplier in northwindEntities.Suppliers on customer.Country equals supplier.Country select new { CustomerName = customer.CompanyName, Supplier = supplier.CompanyName, Country = customer.Country }; Logger.PrintQueries(customerSupplier); //foreach (var item in customerSupplier) //{ // Console.WriteLine(item); //} }
static void Main() { NorthwindEntities northwindEntities = new NorthwindEntities(); var orders = from o in northwindEntities.Orders where o.Customer.Country == "UK" select o; Logger.PrintQueries(orders); foreach (var item in orders) { Console.WriteLine("Order Dates To UK: {0}", item.OrderDate.Value.ToShortDateString()); } }
static void Main() { using (NorthwindEntities northwindEntities = new NorthwindEntities()) { //Joining two Entities JoinCustomerWithSupplier(northwindEntities); JoinCustomerWithSupplierExtentedMethods(northwindEntities); //Grouping //GroupCustomersByCountry(northwindEntities); //GroupCustomersByCountryExtentedMethods(northwindEntities); } }
static void GroupCustomersByCountry(NorthwindEntities northwindEntities) { var groupedCustomers = from customer in northwindEntities.Customers group customer by customer.Country; Logger.PrintQueries(groupedCustomers); //foreach (var group in groupedCustomers) //{ // Console.WriteLine(group.Key); // foreach (var item in group) // { // Console.WriteLine(item.CompanyName); // } // Console.WriteLine(Logger.SeparatorLine); //} }
static void Main() { using (NorthwindEntities northwindEntities = new NorthwindEntities()) { // Creating CreateNewProduct(northwindEntities, "newProduct", false); // Updating //Product productToUpdate = GetProductById(northwindEntities,15); //if (productToUpdate != null) //{ // productToUpdate.ProductName += " changed"; // Update(northwindEntities); //} // Deleting //Product productToDelete = GetProductById(northwindEntities, 16); //DeleteProduct(northwindEntities, productToDelete); } }
static void Main() { using (NorthwindEntities northwindEntities = new NorthwindEntities()) { string queryString = @"SELECT VALUE Customer FROM NorthwindEntities.Customers AS Customer WHERE Customer.City=='London'"; var customerQuery = new ObjectQuery<Customer>(queryString, northwindEntities); Logger.PrintQueries(customerQuery); foreach (Customer customer in customerQuery) { Console.WriteLine("Company Name: {0}\n Contact Name: {1}\n City: {2}\n{3}", customer.CompanyName, customer.ContactName, customer.City, Logger.SeparatorLine); } } }
static void Main() { NorthwindEntities northwindEntities = new NorthwindEntities(); // Used to attach logs to the console // Logger.PrintQueries(northwindEntities); IQueryable<Customer> customers = from c in northwindEntities.Customers where c.City == "London" select c; Logger.PrintQueries(customers); Console.WriteLine("The query is still not generated and executed."); foreach (var item in customers) { Console.WriteLine("Company: {0}, Phone: {1}", item.CompanyName, item.Phone); } }
static void Update(NorthwindEntities northwindEntities) { northwindEntities.SaveChanges(); }
static Product GetProductById(NorthwindEntities northwindEntities, int productId) { return northwindEntities.Products.FirstOrDefault(product => product.ProductID == productId); }
static void DeleteProduct(NorthwindEntities northwindEntities, Product product) { northwindEntities.Products.DeleteObject(product); Update(northwindEntities); }
static void GroupCustomersByCountryExtentedMethods(NorthwindEntities northwindEntities) { var groupedCustomers = northwindEntities.Customers.GroupBy(customer => customer.Country); Logger.PrintQueries(groupedCustomers); }