Ejemplo n.º 1
0
        private static void InsertData()
        {
            var eCars = ProcessCars("fuel.csv");
            var db    = new CarsDB();

            //db.Database.Log = Console.WriteLine;
            foreach (var car in eCars)
            {
                if (!db.Cars.Any())
                {
                    db.Cars.Add(car);
                }
                //db.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges <CarsDB>());
            InsertData();
            QueryData();
            var db   = new CarsDB();
            var cars = ProcessCars("fuel.csv");
            var mfrs = ProcessMfrs("manufacturers.csv");

            var query = db.Cars.OrderByDescending(c => c.Combo).Take(40);

            foreach (var car in query)
            {
                Console.WriteLine($"{ car.Mfr} == {car.Combo }");
            }
            //var xDocument   = new XDocument();                            /// *** Xml samples
            //var xCars       = new XElement("Cars",                        /// All this could be place in a separate method
            //    from car in cars
            //    select new XElement("Cars",
            //                new XAttribute("Name", car.Name),
            //                new XAttribute("Combined", car.Combo),
            //                new XAttribute("Mfr", car.Mfr))
            //        );

            //xDocument.Add(xCars);
            //xDocument.Save("fuel.xml");

            // QueryXML();

            // ****************** Using Entity



            // **************** end Using Entity

            //var query = cars.Where(c => c.Displacement > 5.9 && c.Cylinders > 8).OrderByDescending(c => c.Combo).ThenBy(c => c.Mfr);
            /// ** note this query method works as well
            var query2 =
                from car in cars
                join mfr in mfrs
                on car.Mfr equals mfr.Name
                orderby car.Combo descending, car.Mfr ascending
                select new //sets a new 'anonymous type'. will only pull these values into the cars query
            {
                mfr.HQ,
                car.Mfr,
                car.Name,
                car.Combo
            };
            // *** below is a join statement. Use the outer join with the smaller file size. m symbolizes the Manufacturer data source
            var query3 = cars.Join(mfrs,
                                   c => c.Mfr,
                                   m => m.Name,
                                   (c, m) => new
            {
                m.HQ,
                c.Name,
                c.Mfr,
                c.Combo
            })
                         .OrderByDescending(c => c.Combo)
                         .ThenBy(c => c.Mfr);

            //foreach (var car in query3)
            //{
            //    Console.WriteLine($" {car.HQ} {car.Mfr} {car.Name} MPG: {car.Combo}"); //see note above about the join anonymous query
            //}
            var query4 = from car in cars
                         group car by car.Mfr;
            //foreach (var result in query4)
            //{
            //    Console.WriteLine($"{(result.Key).PadRight(35)} ||  num of cars in group: {result.Count()}");
            //}
            //foreach (var group in query4)
            //{
            //    Console.WriteLine(group.Key);
            //    foreach (var car in group.OrderByDescending(c => c.Combo).Take(4))
            //    {
            //        Console.WriteLine($"{(car.Name).PadLeft(20)} {(car.Combo)}");
            //    }
            //}
        }