Beispiel #1
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                var phones = db.Phones.Where(p => p.Company.Name == "Samsung");
                foreach (Phone p in phones)
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);

                var phones2 = db.Phones.OrderBy(p => p.Name);
                Console.WriteLine("phones2:");
                foreach (Phone p in phones2)
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
                var phones3 = from p in db.Phones
                             orderby p.Name
                             select p;
                Console.WriteLine("phones3:");
                foreach (Phone p in phones3)
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);

                var phones4 = db.Phones.OrderByDescending(p => p.Name);
                Console.WriteLine("phones4:");
                foreach (Phone p in phones4)
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
                var phones5 = db.Phones
                    .Select(p => new { Name = p.Name, Company = p.Company.Name, Price = p.Price })
                    .OrderBy(p => p.Price)
                    .ThenBy(p => p.Company);
                Console.WriteLine("phones5:");
                foreach (var p in phones5)
                    Console.WriteLine("{0}.{1} - {2}", p.Company, p.Name, p.Price);
            }
            Console.ReadLine();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                int number1 = db.Phones.Count();
                // найдем кол-во моделей, которые в названии содержат Samsung
                int number2 = db.Phones.Count(p => p.Name.Contains("Samsung"));

                Console.WriteLine(number1);
                Console.WriteLine(number2);

                int minPrice = db.Phones.Min(p => p.Price);
                // максимальная цена
                int maxPrice = db.Phones.Max(p => p.Price);
                // средняя цена на телефоны фирмы Samsung
                double avgPrice = db.Phones.Where(p => p.Company.Name == "Samsung")
                                    .Average(p => p.Price);

                Console.WriteLine(minPrice);
                Console.WriteLine(maxPrice);
                Console.WriteLine(avgPrice);

                // суммарная цена всех моделей
                int sum1 = db.Phones.Sum(p => p.Price);
                // суммарная цена всех моделей фирмы Samsung
                int sum2 = db.Phones.Where(p => p.Name.Contains("Samsung"))
                                    .Sum(p => p.Price);
                Console.WriteLine(sum1);
                Console.WriteLine(sum2);
            }
            Console.ReadLine();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                int number1 = db.Phones.Count();
                // найдем кол-во моделей, которые в названии содержат Samsung
                int number2 = db.Phones.Count(p => p.Name.Contains("Samsung"));

                Console.WriteLine(number1);
                Console.WriteLine(number2);

                int minPrice = db.Phones.Min(p => p.Price);
                // максимальная цена
                int maxPrice = db.Phones.Max(p => p.Price);
                // средняя цена на телефоны фирмы Samsung
                double avgPrice = db.Phones.Where(p => p.Company.Name == "Samsung")
                                  .Average(p => p.Price);

                Console.WriteLine(minPrice);
                Console.WriteLine(maxPrice);
                Console.WriteLine(avgPrice);

                // суммарная цена всех моделей
                int sum1 = db.Phones.Sum(p => p.Price);
                // суммарная цена всех моделей фирмы Samsung
                int sum2 = db.Phones.Where(p => p.Name.Contains("Samsung"))
                           .Sum(p => p.Price);
                Console.WriteLine(sum1);
                Console.WriteLine(sum2);
            }
            Console.ReadLine();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                var phones = db.Phones.Where(p => p.Price < 25000)
                    .Union(db.Phones.Where(p => p.Name.Contains("Samsung")));
                foreach (var item in phones)
                    Console.WriteLine(item.Name);
                Console.WriteLine("result:");
                var result = db.Phones.Select(p => new { Name = p.Name })
                    .Union(db.Companies.Select(c => new { Name = c.Name }));
                foreach (var item in result)
                    Console.WriteLine(item.Name);
                //Пересечение
                Console.WriteLine("Пересечение:");
                var phones2 = db.Phones.Where(p => p.Price < 25000)
                    .Intersect(db.Phones.Where(p => p.Name.Contains("Samsung")));
                foreach (var item in phones)
                    Console.WriteLine(item.Name);
                //Разность
                Console.WriteLine("Разность:");
                var selector1 = db.Phones.Where(p => p.Price < 25000); // Samsung Galaxy S4, Samsung Galaxy S4, iPhone S4
                var selector2 = db.Phones.Where(p => p.Name.Contains("Samsung")); // Samsung Galaxy S4, Samsung Galaxy S4
                var phones3 = selector1.Except(selector2); // результат -  iPhone S4

                foreach (var item in phones3)
                    Console.WriteLine(item.Name);
            }
            Console.ReadLine();
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
 
                var phones = db.Phones.Join(db.Companies, // второй набор
                    p => p.CompanyId, // свойство-селектор объекта из первого набора
                    c => c.Id, // свойство-селектор объекта из второго набора
                    (p, c) => new // результат
                    {
                        Name = p.Name,
                        Company = c.Name,
                        Price = p.Price
                    });
                foreach (var p in phones)
                    Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);

                Console.WriteLine("phones2:");
                var phones2 = from p in db.Phones
                             join c in db.Companies on p.CompanyId equals c.Id
                             select new { Name = p.Name, Company = c.Name, Price = p.Price };
                foreach (var p in phones)
                    Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);
            }
            Console.ReadLine();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                var phones = db.Phones.Join(db.Companies,     // второй набор
                                            p => p.CompanyId, // свойство-селектор объекта из первого набора
                                            c => c.Id,        // свойство-селектор объекта из второго набора
                                            (p, c) => new     // результат
                {
                    Name    = p.Name,
                    Company = c.Name,
                    Price   = p.Price
                });
                foreach (var p in phones)
                {
                    Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);
                }

                Console.WriteLine("phones2:");
                var phones2 = from p in db.Phones
                              join c in db.Companies on p.CompanyId equals c.Id
                              select new { Name = p.Name, Company = c.Name, Price = p.Price };
                foreach (var p in phones)
                {
                    Console.WriteLine("{0} ({1}) - {2}", p.Name, p.Company, p.Price);
                }
            }
            Console.ReadLine();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                Phone firstPhone = db.Phones.FirstOrDefault();
                firstPhone.Name = "Samsung Galaxy Ace 2";
                db.SaveChanges();

                List<Phone> phones = db.Phones.ToList();
                foreach (Phone ph in phones)
                {
                    Console.WriteLine("Name - {0}, Price - {1}", ph.Name, ph.Price);
                }
                
                
                Console.WriteLine();
                Console.WriteLine("With AsNoTracking method:");
                Phone firstPhone2 = db.Phones.AsNoTracking().FirstOrDefault();
                firstPhone2.Name = "Samsung Galaxy Ace 3";
                db.SaveChanges();

                List<Phone> phones2 = db.Phones.AsNoTracking().ToList();
                foreach (Phone ph in phones2)
                {
                    Console.WriteLine("Name - {0}, Price - {1}", ph.Name, ph.Price);
                }
            }
            Console.ReadLine();
        }
Beispiel #8
0
 static void Main(string[] args)
 {
     using (PhoneContext db = new PhoneContext())
     {
         var phones = db.Phones.Where(p => p.Company.Name == "Samsung");
         foreach (Phone p in phones)
         {
             Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
         }
         var result = from phone in db.Phones
                      join company in db.Companies on phone.CompanyId equals company.Id
                      join country in db.Countries on company.CountryId equals country.Id
                      select new
         {
             Name    = phone.Name,
             Company = company.Name,
             Price   = phone.Price,
             Country = country.Name
         };
         foreach (var p in result)
         {
             Console.WriteLine("Name - {0}, Company - {1}, Price - {2}, Country- {3}", p.Name, p.Company, p.Price, p.Country);
         }
     }
     Console.ReadLine();
 }
Beispiel #9
0
 static void Main(string[] args)
 {
     using (PhoneContext db = new PhoneContext())
     {
         var phones = db.Phones.Where(p => p.Company.Name == "Samsung");
         foreach (Phone p in phones)
             Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
     }
     Console.ReadLine();
 }
Beispiel #10
0
 static void Main(string[] args)
 {
     using (PhoneContext db = new PhoneContext())
     {
         var phones = db.Phones.Where(p => p.Company.Name == "Samsung");
         foreach (Phone p in phones)
         {
             Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
         }
     }
     Console.ReadLine();
 }
Beispiel #11
0
 static void Main(string[] args)
 {
     using (PhoneContext db = new PhoneContext())
     {
         var groups = from p in db.Phones
                      group p by p.Company.Name;
         foreach (var g in groups)
         {
             Console.WriteLine(g.Key);
             foreach (var p in g)
                 Console.WriteLine("{0} - {1}", p.Name, p.Price);
             Console.WriteLine();
         }
     }
     Console.ReadLine();
 }
Beispiel #12
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                int         id         = 3;
                IEnumerable phoneIEnum = db.Phones;
                phoneIEnum = phoneIEnum.Where(p => p.Id > id);

                //IQueryable phoneIQuer = db.Phones;
                //phoneIQuer = phoneIQuer.Where(p => p.Id > id);

                //IEnumerable phoneIEnum = db.Phones.Where(p => p.Id > id);

                //IQueryable phoneIQuer = db.Phones.Where(p => p.Id > id);
            }
            Console.ReadLine();
        }
Beispiel #13
0
 static void Main(string[] args)
 {
     using (PhoneContext db = new PhoneContext())
     {
         var groups = from p in db.Phones
                      group p by p.Company.Name;
         foreach (var g in groups)
         {
             Console.WriteLine(g.Key);
             foreach (var p in g)
             {
                 Console.WriteLine("{0} - {1}", p.Name, p.Price);
             }
             Console.WriteLine();
         }
     }
     Console.ReadLine();
 }
Beispiel #14
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                var phones = db.Phones.Where(p => p.Company.Name == "Samsung");
                foreach (Phone p in phones)
                {
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
                }

                var phones2 = db.Phones.OrderBy(p => p.Name);
                Console.WriteLine("phones2:");
                foreach (Phone p in phones2)
                {
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
                }
                var phones3 = from p in db.Phones
                              orderby p.Name
                              select p;
                Console.WriteLine("phones3:");
                foreach (Phone p in phones3)
                {
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
                }

                var phones4 = db.Phones.OrderByDescending(p => p.Name);
                Console.WriteLine("phones4:");
                foreach (Phone p in phones4)
                {
                    Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
                }
                var phones5 = db.Phones
                              .Select(p => new { Name = p.Name, Company = p.Company.Name, Price = p.Price })
                              .OrderBy(p => p.Price)
                              .ThenBy(p => p.Company);
                Console.WriteLine("phones5:");
                foreach (var p in phones5)
                {
                    Console.WriteLine("{0}.{1} - {2}", p.Company, p.Name, p.Price);
                }
            }
            Console.ReadLine();
        }
Beispiel #15
0
 static void Main(string[] args)
 {
     using (PhoneContext db = new PhoneContext())
     {
         var phones = db.Phones.Where(p => p.Company.Name == "Samsung");
         foreach (Phone p in phones)
             Console.WriteLine("{0}.{1} - {2}", p.Id, p.Name, p.Price);
         var result = from phone in db.Phones
                      join company in db.Companies on phone.CompanyId equals company.Id
                      join country in db.Countries on company.CountryId equals country.Id
                      select new
                      {
                          Name = phone.Name,
                          Company = company.Name,
                          Price = phone.Price,
                          Country = country.Name
                      };
         foreach (var p in result)
             Console.WriteLine("Name - {0}, Company - {1}, Price - {2}, Country- {3}", p.Name, p.Company, p.Price, p.Country);
     }
     Console.ReadLine();
 }
Beispiel #16
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                int id = 3;
                IEnumerable phoneIEnum = db.Phones;
                phoneIEnum = phoneIEnum.Where(p => p.Id > id);

                //IQueryable phoneIQuer = db.Phones;
                //phoneIQuer = phoneIQuer.Where(p => p.Id > id);

                //IEnumerable phoneIEnum = db.Phones.Where(p => p.Id > id);
 
                //IQueryable phoneIQuer = db.Phones.Where(p => p.Id > id);
                 

                
                
                
            }
            Console.ReadLine();
        }
Beispiel #17
0
        static void Main(string[] args)
        {
            using (PhoneContext db = new PhoneContext())
            {
                var phones = db.Phones.Where(p => p.Price < 25000)
                             .Union(db.Phones.Where(p => p.Name.Contains("Samsung")));
                foreach (var item in phones)
                {
                    Console.WriteLine(item.Name);
                }
                Console.WriteLine("result:");
                var result = db.Phones.Select(p => new { Name = p.Name })
                             .Union(db.Companies.Select(c => new { Name = c.Name }));
                foreach (var item in result)
                {
                    Console.WriteLine(item.Name);
                }
                //Пересечение
                Console.WriteLine("Пересечение:");
                var phones2 = db.Phones.Where(p => p.Price < 25000)
                              .Intersect(db.Phones.Where(p => p.Name.Contains("Samsung")));
                foreach (var item in phones)
                {
                    Console.WriteLine(item.Name);
                }
                //Разность
                Console.WriteLine("Разность:");
                var selector1 = db.Phones.Where(p => p.Price < 25000);            // Samsung Galaxy S4, Samsung Galaxy S4, iPhone S4
                var selector2 = db.Phones.Where(p => p.Name.Contains("Samsung")); // Samsung Galaxy S4, Samsung Galaxy S4
                var phones3   = selector1.Except(selector2);                      // результат -  iPhone S4

                foreach (var item in phones3)
                {
                    Console.WriteLine(item.Name);
                }
            }
            Console.ReadLine();
        }