示例#1
0
        static void Main(string[] args)
        {
            var db = new Kontekst();

            db.Database.EnsureCreated();
            db.Zajecias.Add(new Zajecia()
            {
                Nazwa = "P4x",
                GodzinaRozpozcecia = new DateTime(2020, 1, 1, 13, 30, 00)
            });
            db.SaveChanges();

            var zajecia = db.Zajecias.Where(x => x.ID > 2);

            foreach (var item in zajecia)
            {
                Console.WriteLine($"{ item.ID}. { item.Nazwa}. { item.GodzinaRozpozcecia}");
            }

            var zajeciaDoZmiany = db.Zajecias.First(x => x.Nazwa.StartsWith("P"));

            zajeciaDoZmiany.Nazwa = "AM2";
            db.Update(zajeciaDoZmiany);
            db.SaveChanges();


            //first db

            //var nwctx = new NorthwindContext();
            //var join = nwctx.Orders.Include(x => x.Customer);
            //var query = join.Take(5);
            //foreach (var item in query)
            //{
            //    Console.WriteLine($"{item.Customer.CustomerId} : {item.OrderId} : {item.OrderDate}");
            //}

            //foreach (var order in nwctx.order.Includex(x => x.Customer))
            //{
            //    Console.WriteLine(order.OrderId);
            //}

            //foreach (var item in nwctx.PozycjeZamowienia.Include(x => x))
            //{
            //    Console.WriteLine($"Id zamówienia:{item.IdZamówienia} Id produktu:{item.IdProduktu}   " +
            //        $"Nazwa produktu:{item.IdProduktuNavigation.NazwaProduktu} Cena jednostkowa:{item.IdProduktuNavigation.CenaJednostkowa} " +
            //        $"Ilość: {item.Ilość}");
            //}
            var NorthwindContext = new NorthwindContext();
            var join             = NorthwindContext.Orders.Include(x => x.Customer);
            var query            = join.Take(5);

            foreach (var item in query)
            {
                Console.WriteLine($"{item.Customer.CustomerId} : {item.OrderId} : {item.OrderDate}");
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            var db = new Kontekst();
            db.Database.EnsureCreated();
            db.Zajecias.Add(new Zajecia() { Nazwa = "p4", GodzinaRozpoczecia = new DateTime(2020, 1, 1, 12, 30, 0) }); //nie mozna wstawiac wlasnego id
            db.SaveChanges();

            var zajecia = db.Zajecias.Where(x => x.Id > 2);

            foreach (var item in zajecia)
            {
                Console.WriteLine($"{item.Id}. {item.Nazwa} {item.GodzinaRozpoczecia.ToShortTimeString()}");
            }

            var zajeciaDoZmiany = db.Zajecias.First(x => x.Nazwa.StartsWith("p"));
            zajeciaDoZmiany.Nazwa = "Am2";
            db.Update(zajeciaDoZmiany);
            db.SaveChanges();



            using (var connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
            {
                var nxwc = new Northwind();
                 

                foreach (var order in nxwc.Orders.Include(x => x.Customer))
                {
                    Console.WriteLine(order.Customer.CompanyName);
                }


                foreach (var item in nxwc.Orders
                    .Include(x => x.CenaJednostkowa)
                    .Include(x => x.IdZamowienia))
                {

                }
               
            }


            


           

          


            } 
示例#3
0
        static void Main(string[] args)
        {
            using var baza = new Kontekst();
            baza.Database.EnsureCreated();
            var nowyStudent = new Student()
            {
                Imie = "Jan", Nazwisko = "Kowalski"
            };

            baza.Students.Add(nowyStudent);
            baza.SaveChanges();
            var studenci = baza.Students.Where(x => x.Imie == "Jan");

            foreach (var item in studenci)
            {
                Console.WriteLine($"{item.Id}. {item.Nazwisko}");
            }
            var student = baza.Students.Where(x => x.Id == 2).First();

            student.Imie = "Piotr";
            baza.Students.Update(student);
            baza.SaveChanges();
            var stud1 = baza.Students.Where(x => x.Imie == "Piotr");

            baza.Remove(nowyStudent);
            baza.SaveChanges();
            Console.WriteLine();
            Console.WriteLine();
            using var baza2 = new NorthwindContext();
            var klient = baza2.Customers.Where(x => x.CustomerId == "BOLID");

            foreach (var item in klient)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Podaj firmę:");
            string firma;

            firma = Console.ReadLine();
            var customers     = baza2.Customers.Where(x => x.CustomerId == firma).ToList();
            var order         = baza2.Orders.Where(x => x.CustomerId == firma).ToList();
            var ordersDetails = baza2.OrderDetails.Select(x => new OrderDetails()).ToList();
            var products      = baza2.Products.Select(x => new { x.ProductId, x.ProductName }).OrderBy(x => x.ProductId)
                                .ToList();
            var queryJoining = customers.Join(order, x => x.CustomerId, d => d.CustomerId,
                                              (customers, order) => new { CustId = customers.CustomerId, OrdId = order.OrderId }).ToList();

            foreach (var item in queryJoining)
            {
                Console.WriteLine(item.CustId + " " + item.OrdId);
            }
            Console.WriteLine();
            List <Customers>    cust   = baza2.Customers.Where(x => x.CustomerId == firma).ToList();
            List <Orders>       ord    = baza2.Orders.ToList();
            List <OrderDetails> ordDet = baza2.OrderDetails.ToList();
            List <Products>     prod   = baza2.Products.ToList();

            var querryToCreateSuperProductResultStat =
                from c in cust
                join o in ord on c.CustomerId equals o.CustomerId into tab1
                from t1 in tab1.ToList()
                join oD in ordDet on t1.OrderId equals oD.OrderId into tab2
                from t2 in tab2.ToList()
                join p in prod on t2.ProductId equals p.ProductId into finTab
                from x in finTab.ToList()
                select new ViewModel
            {
                IdKlient  = c.CustomerId,
                IdProd    = x.ProductId,
                NazwaProd = x.ProductName
            };

            foreach (var item in querryToCreateSuperProductResultStat)
            {
                Console.WriteLine($"{item.IdKlient} - {item.IdProd} - {item.NazwaProd}");
            }
        }