Example #1
0
 private void button2_Click(object sender, EventArgs e)
 {
     using (var context = new ProdContext())
     {
         this.label3.Text = (from c in context.Categories
                             select c)
                            .ToList()
                            .Count()
                            .ToString();
         this.label5.Text = (from p in context.Products select p)
                            .ToList()
                            .Count()
                            .ToString();
         this.label7.Text = (from c in context.Customers select c)
                            .ToList()
                            .Count()
                            .ToString();
     }
 }
Example #2
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _context = new ProdContext();
            _context.Categories.Load();

            this.categoryBindingSource.DataSource =
                _context.Categories.Local.ToBindingList();

            this.label3.Text = (from c in _context.Categories
                                select c)
                               .ToList()
                               .Count()
                               .ToString();
            this.label5.Text = (from p in _context.Products select p)
                               .ToList()
                               .Count()
                               .ToString();
            this.label7.Text = (from c in _context.Customers select c)
                               .ToList()
                               .Count()
                               .ToString();
        }
Example #3
0
        public static void PrintProductsInCategoriesJoin()
        {
            using (var context = new ProdContext())
            {
                var query = from c in context.Categories
                            join p in context.Products
                            on c.CategoryId equals p.CategoryId into productGroup
                            select new
                {
                    CategoryId   = c.CategoryId,
                    CategoryName = c.Name,
                    ProductCount = productGroup.Count(),
                    Products     = productGroup
                };

                var query2 = context.Categories.GroupJoin(context.Products, p => p.CategoryId, c => c.CategoryId,
                                                          (category, productGroup) => new
                {
                    CategoryId   = category.CategoryId,
                    CategoryName = category.Name,
                    ProductCount = productGroup.Count(),
                    Products     = productGroup
                });

                foreach (var category in query2)
                {
                    Console.WriteLine("CategoryID: {0}", category.CategoryId);
                    Console.WriteLine("Product count: {0}", category.ProductCount);
                    foreach (var prod in category.Products)
                    {
                        Console.WriteLine(" ProductID: {0}", prod.ProductId);
                    }
                    Console.WriteLine("");
                }
            }
        }
Example #4
0
        // Save and close
        private void button1_Click(object sender, EventArgs e)
        {
            int belongsToCategory = 0;

            using (var context = new ProdContext())
            {
                belongsToCategory = context.Categories
                                    .Where(c => c.Name == (string)comboBox1.SelectedItem)
                                    .Select(c => c.CategoryId)
                                    .FirstOrDefault <int>();
            }
            if (belongsToCategory == 0)
            {
                MessageBox.Show("Please select a category");
                return;
            }
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please provide a name");
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Please provide the unit price");
                return;
            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("Please provide the number of units in stock");
                return;
            }
            var newProduct = new Product
            {
                CategoryId = belongsToCategory,
                Name       = textBox1.Text
            };

            try
            {
                newProduct.UnitPrice = Decimal.Parse(textBox2.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Unit price must be a decimal value");
                return;
            }
            try
            {
                newProduct.UnitsInStock = Int32.Parse(textBox3.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("The number of units in stock must be an integer");
                return;
            }
            using (var context = new ProdContext())
            {
                context.Products.Add(newProduct);
                context.SaveChanges();
            }
            Hide();
            DestroyHandle();
        }