示例#1
0
        private static void QueryData()
        {
            var db = new Part8_CarDb();

            db.Database.Log = Console.WriteLine;

            var query1 =
                db.Cars.Where(c => c.Manufacturer == "BMW")
                .OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name)
                .Take(10);

            Console.WriteLine("Take only take BMW cars");
            foreach (var car in query1)
            {
                Console.WriteLine($"\t{car.Name}: {car.Combined}");
            }

            var query2 =
                db.Cars.Where(c => c.Manufacturer == "BMW")
                .OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name)
                .Take(10)
                .Select(c => new { Name = c.Name.ToUpper() })
                .ToList();

            Console.WriteLine("Change to Upper");
            foreach (var car in query2)
            {
                Console.WriteLine($"\t{car.Name}");
            }


            var query =
                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)
            };


            foreach (var group in query)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t{car.Name}: {car.Combined}");
                }
            }
        }
示例#2
0
        private static void InsertData()
        {
            var cars = ProcessCars("fuel.csv");
            var db   = new Part8_CarDb();

            if (!db.Cars.Any())
            {
                foreach (var car in cars)
                {
                    db.Cars.Add(car);
                }
                db.SaveChanges();
            }
        }