示例#1
0
        private static void InsertCarsintoSQL_UsingEntityFramework(List <Car> cars, List <Manufacturer> manufacturers)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges <CarDB>());
            var db = new CarDB();

            //db.Database.Log = Console.WriteLine;
            if (!db.cars.Any())
            {
                foreach (var car in cars)
                {
                    db.cars.Add(car);
                }

                db.SaveChanges();
            }

            Console.WriteLine("Cars DB created and cars are populated");
            var db_M = new ManufacturersDB();

            if (!db_M.Manufacturers.Any())
            {
                foreach (var manufacturer in manufacturers)
                {
                    db_M.Manufacturers.Add(manufacturer);
                }

                db_M.SaveChanges();
            }

            Console.WriteLine("Manufacturers DB created and Manufacturers are populated");

            Console.ReadLine();
        }
示例#2
0
        private static void InsertData()
        {
            var cars = ProcessCars("fuel.csv");
            var db   = new CarDB();

            db.Database.Log = Console.WriteLine;

            if (!db.Cars.Any())
            {
                foreach (var car in cars)
                {
                    db.Cars.Add(car);
                }
            }
            db.SaveChanges();
        }
示例#3
0
        private static void QueryingFromDB_UsingEF()
        {
            var db = new CarDB();

            db.Database.Log = Console.WriteLine;

            //var query = from car in db.cars
            //            orderby car.FE_Comb descending, car.Carline ascending
            //            select car;

            var query2 = db.cars.OrderByDescending(c => c.FE_Comb)
                         .ThenBy(c => c.Carline)
                         .ToList()
                         .Take(10);

            foreach (var car in query2)
            {
                Console.WriteLine($"{car.Carline} : {car.FE_Comb}");
            }
        }
示例#4
0
        private static void QueryData()
        {
            var db = new CarDB();

            db.Database.Log = Console.WriteLine;

            var query =
                //1st
                //from car in db.Cars
                //orderby car.Combined descending, car.Name ascending
                //select car;

                //2nd
                //db.Cars.Where(c => c.Manufacturer == "BMW")
                //.OrderByDescending(c => c.Combined)
                //.ThenBy(c => c.Name)
                //.Take(10)
                //.Select(c => new { Name = c.Name.Split(' '});
                //once u select .ToList() it becomes concrete data, not deferred execution


                //3rd
                //db.Cars.GroupBy(c => c.Manufacturer)
                //        .Select(g => new
                //        {
                //            Name = g.Key,
                //            Cars = g.OrderByDescending(c => c.Combined).Take(2)
                //        });

                //4th
                from car in db.Cars
                group car by car.Manufacturer into manufacturer
                select new
            {
                Name = manufacturer.Key,
                Cars = (from car in manufacturer
                        orderby car.Combined descending
                        select car).Take(2)
            };

            //1st
            //foreach (var car in query.Take(10))
            //{
            //    Console.WriteLine($"{car.Name} : {car.Combined}");
            //}

            //2nd
            //foreach (var item in query)
            //{
            //    Console.WriteLine(item.Name);
            //}

            //3rd
            foreach (var group in query)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t{car.Name} : {car.Combined}");
                }
            }
        }