Ejemplo n.º 1
0
        static void Task2()
        {
            CarSalesBook carSalesBook = new CarSalesBook();

            var cars = carSalesBook.ReadCarsFromFile();

            Console.WriteLine("Which are TOP 3 makes whose sales with regard to the amount of sold cars in 2014?");
            PrintMakes(cars.OrderBy(car => - car.Sales2014).Take(3));

            Console.WriteLine("\nThe sales of which makes increased by at least 50% in 2015 comparing to 2014?");
            PrintMakes(cars.Where(car => car.Sales2015 >= 1.5 * car.Sales2014));

            Console.WriteLine("\nWhich 3 makes opens the second ten of the sales ranking in 2015");
            PrintMakes(cars.OrderBy(car => - car.Sales2015).Skip(10).Take(3));

            Console.WriteLine("\nWhat are the totals of sold cars in 2014 and 2015");
            Console.WriteLine("Total 2014: " + cars.Sum(n => n.Sales2014));
            Console.WriteLine("Total 2015: " + cars.Sum(n => n.Sales2015));
            Console.WriteLine("Total 2014 and 2015: " + (cars.Sum(n => n.Sales2014) + cars.Sum(n => n.Sales2015)));

            Console.WriteLine("\nCreate a list that contains TOP10 and LAST 10 cars with respect to sales in 2015.");
            IEnumerable <Car> result = cars.OrderBy(car => - car.Sales2015).Take(10).Union(
                cars.OrderBy(car => - car.Sales2015).Reverse().Take(10).Reverse());

            PrintMakes(result);

            string path = @"..\..\cars.xml";

            SaveToXML(result, path);
            Console.WriteLine("\nSaved results from previous query to XML.");

            Console.WriteLine("\nLoaded cars from XML:");
            List <Car> loadedCars = ReadFromXML(path);

            PrintMakes(loadedCars);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            CarSalesBook carSalesBook = new CarSalesBook();

            carSalesBook._cars = carSalesBook.ReadCarsFromFile();

            /*
             * foreach (var item in carSalesBook._cars)
             * {
             *  Console.WriteLine(item.Make);
             * }
             */

            //Top 3
            var top3 = (from item in carSalesBook._cars
                        orderby item.Sales2014 descending
                        select item).Take(3);

            //least50%
            var least50 = from item in carSalesBook._cars
                          where item.Sales2015 >= item.Sales2014 * 1.5
                          select item;

            //3makesOpens
            var makesOpens = (from item in carSalesBook._cars
                              orderby item.Sales2015 descending
                              select item).Skip(10).Take(3);

            //total
            var total = from item in carSalesBook._cars
                        group item by 1
                        into g
                        select new
            {
                SumTotal = g.Sum(x => x.Sales2014),
                SumDone  = g.Sum(x => x.Sales2015)
            };

            //top10last10
            var tmp = from item in carSalesBook._cars
                      orderby item.Sales2015 descending
                      select item;
            var top10last10 = tmp.Take(10).Concat(tmp.Skip(Math.Max(0, tmp.Count() - 10)));

            //saveToXML
            var filename         = "save.xml";
            var currentDirectory = Directory.GetCurrentDirectory();
            var filepath         = Path.Combine(currentDirectory, filename);

            var xml = new XElement("Cars", carSalesBook._cars.Select(x => new XElement("Car",
                                                                                       new XElement("Make", x.Make),
                                                                                       new XElement("Sales2014", x.Sales2014),
                                                                                       new XElement("Sales2015", x.Sales2015))));

            xml.Save(filepath);

            //loadFromXML
            XElement loadingElement = XElement.Load(filepath);
            var      carsLoad       = from item in loadingElement.Descendants("Car")
                                      select item;

            /*
             * foreach (var row in carsLoad)
             * {
             *  Console.WriteLine(row.Element("Make").Value + ", " + row.Element("Sales2014").Value + ", " + row.Element("Sales2015").Value);
             * }
             */
            /*
             * List<Process> list = new List<Process>();
             * var proc1 = Process.Start("https://www.google.com");
             * list.Add(proc1);
             * Console.WriteLine(list.MemorySum());
             */

            //leap year method
            Func <int, bool> leapYear = x => (x % 4) == 0;

            Console.WriteLine(leapYear(2001));

            /*
             * OwnGenericCollection<String> ogc = new OwnGenericCollection<string>();
             * Console.WriteLine(ogc.IsEmpty());
             * ogc.Add("1");
             * ogc.Add("2");
             * ogc.Add("3");
             * ogc.Add("4");
             * ogc.Add("5");
             * Console.WriteLine(ogc.Get(0));
             * Console.WriteLine(ogc.Get(4));
             * Console.WriteLine(ogc.IsEmpty());
             */
            Console.ReadLine();
        }