static void Main(string[] args) { var productList = new List<Product>(); productList.Add(new Product() { ProductId = 50, CategoryId = 90, Name="Samsung" }); productList.Add(new Product() { ProductId = 90, CategoryId = 110, Name = "LG" }); productList.Add(new Product() { ProductId = 150, CategoryId = 150, Name = "Toshiba" }); productList.Add(new Product() { ProductId = 60, CategoryId = 200, Name="Sony" }); productList.Add(new Product() { ProductId = 75, CategoryId = 190, Name = "Sharp" }); var someCategory = new Category() { CategoryID = 100, CategoryName = "TV" }; foreach (var product in productList) { product.Category = someCategory; } var sortedProductList = GetProducts(productList); Console.WriteLine("Products with ID between 1-100"); foreach (var product in sortedProductList) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Product Name: {0}; ID: {1}",product.Name,product.ProductId); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); var categoryList = new List<Category>(); categoryList.Add(new Category() { CategoryID = 100, CategoryName = "TV" }); categoryList.Add(new Category() { CategoryID = 90, CategoryName = "Computers" }); categoryList.Add(new Category() { CategoryID = 150, CategoryName = "Audio" }); categoryList.Add(new Category() { CategoryID = 210, CategoryName = "Appliances" }); categoryList.Add(new Category() { CategoryID = 190, CategoryName = "Accesories" }); foreach (var category in categoryList) { category.Products = productList; } var sortedCategoryList = GetCategoryes(categoryList); Console.WriteLine("Categoryes with ID between 101-200"); foreach (var category in categoryList) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Category Name: {0}; Category ID: {1}",category.CategoryName,category.CategoryID); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); var orderList = new List<Order>(); orderList.Add(new Order() { OrderId = 240, Name = "Oreder 1", OrderDate = DateTime.Now, Products = productList }); orderList.Add(new Order() { OrderId = 190, Name = "Order 2", OrderDate = DateTime.Now, Products = sortedProductList }); orderList.Add(new Order() { OrderId = 280, Name = "Order 3", OrderDate = DateTime.Now, Products = productList }); orderList.Add(new Order() { OrderId = 310, Name = "Order 4", OrderDate = DateTime.Now, Products = sortedProductList }); var sortedOrderList = GetOrders(orderList); Console.WriteLine("Orders with ID between 201-300"); foreach (var order in sortedOrderList) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Order Name: {0}; Order ID: {1}",order.Name,order.OrderId); foreach (var product in order.Products) { Console.WriteLine("-{0}",product.Name); } Console.WriteLine(); } Console.WriteLine(); //Queries //returns all products with ids between 15 and 30 productList.Where(p => p.ProductId >= 15 && p.ProductId <= 30); //returns all categories with ids between 105 and 125 categoryList.Where(c => c.CategoryID >= 105 && c.CategoryID <= 125); //returns first 15 orders sorted by order name orderList.OrderBy(o => o.Name).Take(15); //returns first 3 orders which contains a specific productId (e.g. 10) //Orders must be sorted based on OrderDate The print order names. var customOrder = orderList.Where(o => o.Products.Any(p => p.ProductId == 150)).OrderBy(o => o.OrderDate).Take(3).ToList(); foreach (var order in customOrder) { Console.WriteLine(order.Name); } Console.WriteLine(); //returns all product with the name of the category which they belong to //Orders must be sorted based on OrderDate The print order names. var customProductList = productList.OrderBy(p => p.Category.CategoryName); foreach (var product in customProductList) { Console.WriteLine("Product: {0}; Category: {1}",product.Name,product.Category.CategoryName); } Console.WriteLine(); //Create linq query which returns all categories together with their products //Create class CategoryWithProduct which should keep the result List<CategoryWithProduct> categoryWithProduct = categoryList.Select(c => new CategoryWithProduct { CateforyId = c.CategoryID, CategoryName = c.CategoryName, ProductNames = c.Products.Select(p => p.Name).ToList() }).ToList(); foreach (var item in categoryWithProduct) { Console.WriteLine("Name: {0} - ID: {1}",item.CategoryName,item.CateforyId); foreach (var productName in item.ProductNames) { Console.WriteLine("-{0}",productName); } } Console.WriteLine(); //Create linq query which selects all orders together with their products and then print it to the screen. //For every product print its category name as well. Sort the result by orderDate var customOrderList = orderList.OrderBy(o => o.OrderDate).ToList(); foreach (var order in customOrderList) { Console.WriteLine("-{0}",order.Name); foreach (var product in order.Products) { Console.WriteLine("{0} - {1}",product.Category.CategoryName,product.Name); } } }
public void AddCategory(Category category) { categories.Add(category); }
partial void DeleteCategory(Category instance);
partial void UpdateCategory(Category instance);
partial void InsertCategory(Category instance);
/// <summary> /// Main method. /// </summary> public static void Main() { var pr1 = new Product("Chai1", -1, 5); var pr2 = new Product("Chai2", 99, 5); var pr3 = new Product("Chai3", 100, 124); var pr4 = new Product("Chai4", 4, 102); var pr5 = new Product("Chai5", 5, 101); List<Product> products = new List<Product>() { pr1, pr2, pr3, pr4, pr5 }; var c1 = new Category(5, "name1"); var c2 = new Category(125, "name2"); var c3 = new Category(124, "name3"); var c4 = new Category(102, "name4"); var c5 = new Category(100, "name5"); var c6 = new Category(101, "name6"); List<Category> categories = new List<Category>() { c1, c2, c3, c4, c5, c6 }; List<int> prl1 = new List<int>() { 99, 4, 100, -1 }; List<int> prl2 = new List<int>() { 4, 5, -1, 100 }; List<int> prl3 = new List<int>() { 99, 4, 5 }; List<int> prl4 = new List<int>() { 100, 91, 5 }; var or1 = new Order("order 1", 1, prl1, new DateTime(2001, 1, 1)); var or2 = new Order("order 2", 2, prl2, new DateTime(2000, 1, 2)); var or3 = new Order("order 3", 3, prl3, new DateTime(2000, 1, 3)); var or4 = new Order("order 4", 4, prl4, new DateTime(2000, 1, 8)); var or5 = new Order("order 5", 5, prl4, new DateTime(2000, 1, 7)); var or6 = new Order("order 6", 6, prl4, new DateTime(2000, 1, 6)); var or7 = new Order("order 7", 7, prl4, new DateTime(2000, 1, 14)); var or8 = new Order("order 8", 8, prl4, new DateTime(2000, 1, 24)); var or9 = new Order("order 9", 9, prl4, new DateTime(2000, 6, 4)); var or10 = new Order("order 14", 14, prl4, new DateTime(2000, 2, 4)); var or11 = new Order("order 24", 24, prl4, new DateTime(2000, 6, 4)); var or12 = new Order("order 34", 34, prl4, new DateTime(2000, 1, 5)); var or13 = new Order("order 44", 44, prl4, new DateTime(2000, 3, 4)); var or14 = new Order("order 54", 54, prl4, new DateTime(2000, 2, 4)); var or15 = new Order("order 64", 64, prl4, new DateTime(2000, 2, 4)); List<Order> orders = new List<Order>() { or1, or2, or3, or4, or5, or6, or7, or8, or9, or10, or11, or12, or13, or14, or15 }; DataStore ds1 = new DataStore(orders, categories, products); // returns all products with ids between 15 and 30 var prods = ds1.GetProducts().Where(x => x.CategoryId > 15 && x.CategoryId < 30).ToList(); // returns all categories with ids between 105 and 125 var cats = ds1.GetCategories().Where(x => x.CategoryId > 105 && x.CategoryId < 125).ToList(); // returns first 15 orders sorted by order name var ords = ds1.GetOrders().OrderBy(x => x.Name).Take(15); // returns first 3 orders which contains a specific productId (-1) var newprods = ds1.GetOrders().Where(x => x.Products.Contains(-1)).OrderBy(x => x.OrderDate).ToList(); // returns all product with the name of the category which they belong to var prodCats = from prod in ds1.GetProducts() orderby prod.Name select new { Name = prod.Name }; // returns all categories together with their products var catsAndProds = from cat in categories join prod in products on cat.CategoryId equals prod.CategoryId select new { Category = cat.CategoryName, Prod = prod.Name }; // All orders together with their products and then print it to the screen. // For every product print its category name as well. Sort the result by orderDate. var ress = orders.OrderBy(x => x.OrderDate).SelectMany( x => x.Products.Select( y => products.Where( z => z.Id == y).Select( z => new { Order = x.Id, Product = z.Name }).FirstOrDefault())).Where(x => x != null).ToList(); }