Пример #1
0
 static void ShowProducts_NavigationProperties()
 {
     using (var ctx = new ProdContext())
     {
         Console.WriteLine("Available products:");
         foreach (Product p in ctx.Products)
         {
             ctx.Entry(p).Reference(pr => pr.Category).Load();
             if (null != p.Category)
             {
                 Console.WriteLine("{0} {1} belongs to category {2}", p.ProductID, p.Name, p.Category.Name);
             }
             else
             {
                 Console.WriteLine("{0} {1} does not belong to any category", p.ProductID, p.Name, p.Category.Name);
             }
         }
     }
 }
Пример #2
0
        private void logInTile_Click(object sender, EventArgs e)
        {
            var customerName     = companyNameTextBox.Text.Trim();
            var customerPassword = passwordTextBox.Text;

            using (var ctx = new ProdContext())
            {
                var customerToAuthorize = ctx.Customers.Where(c => c.CompanyName == customerName);
                if (customerToAuthorize.Count() > 0 && customerToAuthorize.First().Password == customerPassword)
                {
                    this.Hide();
                    new DashboardForm().ShowDialog();
                    this.Close();
                }
                else
                {
                    showInvalidCredentialsMessageBox("Invalid company name or password.");
                }
            }
        }
Пример #3
0
        static void ShowProducts_Joins()
        {
            using (ProdContext ctx = new ProdContext())
            {
                // Eagerly

                //var products = ctx.Categories
                //    .Join(
                //    ctx.Products,
                //    c => c.CategoryId,
                //    p => p.CategoryId,
                //    (c, p) => new
                //    {
                //        CategoryName = c.Name,
                //        ProductName = p.Name,
                //        CategorySize = c.Products.Count()
                //    })
                //    .OrderBy(cp => cp.CategoryName)
                //    .ToList();

                var products = (from c in ctx.Categories
                                join p in ctx.Products on c.CategoryId equals p.CategoryId
                                orderby c.Name descending
                                select new
                {
                    CategoryName = c.Name,
                    ProductName = p.Name,
                    CategorySize = c.Products.Count()
                })
                               .ToList();
                foreach (var p in products)
                {
                    Console.WriteLine("Product {0} belongs to category {1} with size {2}.", p.ProductName, p.CategoryName, p.CategorySize);
                }
            }
        }