} // end ListProducts public ProductData GetProduct(string productNumber) { // Create a reference to a ProductData object ProductData productData = null; try { // Connect to AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { // Find the first product that matches the specified product number Product matchingProduct = database.Products.First( p => String.Compare(p.ProductNumber, productNumber) == 0); productData = new ProductData() { Name = matchingProduct.Name, ProductNumber = matchingProduct.ProductNumber, Color = matchingProduct.Color, ListPrice = matchingProduct.ListPrice }; // end productData } } catch { // Ignore exceptions in this implementation } // Return the product return productData; } // end GetProduct
public int CurrentStockLevel(string productNumber) { // Obtain the total stock level for the specified product. // The stock level is calculated by summing the quantity of the product // available in all the bins in the ProductInventory table. // The Product and ProductInventory tables are joined over the // ProductID column. int stockLevel = 0; try { // Connect to the AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { // Calculate the sum of all quantities for the specified product stockLevel = (from pi in database.ProductInventories join p in database.Products on pi.ProductID equals p.ProductID where String.Compare(p.ProductNumber, productNumber) == 0 select (int)pi.Quantity).Sum(); } } catch { // Ignore exceptions in this implementation } // Return the stock level return stockLevel; }
public List<string> ListProducts() { // Create a list for holding product numbers List<string> productsList = new List<string>(); // Create a list for holding product numbers try { // Connect to AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { // Fetch the product number of every product in the database var products = from product in database.Products select product.ProductNumber; productsList = products.ToList(); } } catch { // Ignore exceptions in this implementation } // Return the list of product numbers return productsList; } // end ListProducts
public void ChangePrice(string productNumber, decimal price) { // Modify the price of the selected product // If the update is successful then return true, otherwise return false. Product product = null; try { // Connect to the AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { if (!ProductExists(productNumber, database)) return; else { // Find the specified product product = (from p in database.Products where String.Compare(p.ProductNumber, productNumber) == 0 select p).First(); // Change the price for the product product.ListPrice = price; // Save the change back to the database database.SaveChanges(); } } } catch { // If an exception occurs, return false to indicate failure return; } // Notify the client that the price has been changed successfully ProductData productData = new ProductData() { ProductNumber = product.ProductNumber, Name = product.Name, ListPrice = product.ListPrice, Color = product.Color }; raisePriceChangedEvent(productData); }
public bool ChangeStockLevel(string productNumber, short newStockLevel, string shelf, int bin) { // Modify the current stock level of the selected product // in the ProductInventory table. // If the update is successful then return true, otherwise return false. // The Product and ProductInventory tables are joined over the // ProductID column. try { // Connect to the AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { // Find the ProductID for the specified product int productID = (from p in database.Products where String.Compare(p.ProductNumber, productNumber) == 0 select p.ProductID).First(); // Find the ProductInventory object that matches the parameters passed // in to the operation ProductInventory productInventory = database.ProductInventories.First( pi => String.Compare(pi.Shelf, shelf) == 0 && pi.Bin == bin && pi.ProductID == productID); // Update the stock level for the ProductInventory object productInventory.Quantity += newStockLevel; // Save the change back to the database database.SaveChanges(); } } catch { // If an exception occurs, return false to indicate failure return false; } // Return true to indicate success return true; }
public bool ProductExists(string productNumber, AdventureWorksEntities database) { // Check to see whether the specified product exists in the database int numProducts = (from p in database.Products where string.Equals(p.ProductNumber, productNumber) select p).Count(); return numProducts > 0; }
public ProductData GetProduct(string productNumber) { // Create a reference to a ProductData object // This object will be instantiated if a matching product is found ProductData productData = null; try { // Connect to the AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { // Check that the specified product exists if (ProductExists(productNumber, database)) { // Find the first product that matches the specified product number Product matchingProduct = database.Products.First( p => String.Compare(p.ProductNumber, productNumber) == 0); productData = new ProductData() { Name = matchingProduct.Name, ProductNumber = matchingProduct.ProductNumber, Color = matchingProduct.Color, ListPrice = matchingProduct.ListPrice }; } } } catch { // Ignore exceptions in this implementation } // Return the product return productData; }