示例#1
0
        public static void LinqWithXml()
        {
            Console.WriteLine("****LINQ WITH XML****");

            var records = ProcessMethods.ProcessCarFileByExtensionMethod("fuel.csv");

            Console.WriteLine("****Build element oriented xml file with functional construction (foreach version)****");
            var fileName = "fuel.xml";

            XmlMethods.CreateXmlByForeachMethod(records, fileName);

            Console.WriteLine("****Build element oriented xml file with functional construction (query method version)****");
            fileName = "fuel_by_query_method.xml";
            XmlMethods.CreateXmlByQueryMethod(records, fileName);

            Console.WriteLine("****Build element oriented xml file with functional construction (extension method version)****");
            fileName = "fuel_by_extension_method.xml";
            XmlMethods.CreateXmlByExtensionMethod(records, fileName);

            Console.WriteLine("****Build element oriented xml file with functional construction and namespace (extension method version)****");
            var ns = "http://github.com/cars/2021";
            var ex = "http://github.com/cars/2021/ex";

            fileName = "fuel_with_namespace_by_extension_method.xml";
            XmlMethods.CreateXmlWithNamespaceByExtensionMethod(records, fileName, ns, ex);

            Console.WriteLine("****Get xml attribute by elements and namespace where manufacturer is BMW (query method version)****");
            fileName = "fuel_with_namespace_by_extension_method.xml";
            var xAttributes = XmlMethods.GetXmlAttributesByElementsByNamespaceUsingQueryMethod(fileName, "BMW", ns, ex);

            foreach (var name in xAttributes)
            {
                Console.WriteLine(name);
            }

            Console.WriteLine("****Get xml attribute by elements where manufacturer is BMW (query method version)****");
            fileName    = "fuel.xml";
            xAttributes = XmlMethods.GetXmlAttributesByElementsUsingQueryMethod(fileName, "BMW");
            foreach (var name in xAttributes)
            {
                Console.WriteLine(name);
            }

            Console.WriteLine("****Get xml attribute by descendants where manufacturer is BMW (query method version)****");
            fileName    = "fuel.xml";
            xAttributes = XmlMethods.GetXmlAttributesByDescendantsUsingQueryMethod(fileName, "BMW");
            foreach (var name in xAttributes)
            {
                Console.WriteLine(name);
            }
        }
示例#2
0
        public static void LinqOnCars()
        {
            Console.WriteLine("****LINQ ON CARS****");

            Console.WriteLine("****Show 15 car names with extension method syntax****");
            var cars = ProcessMethods.ProcessCarFileByExtensionMethod("fuel.csv");

            foreach (var car in cars.Take(15))
            {
                Console.WriteLine(car.Name);
            }

            Console.WriteLine("****Show 15 car names with query method syntax****");
            cars = ProcessMethods.ProcessCarFileByQueryMethod("fuel.csv");

            foreach (var car in cars.Take(15))
            {
                Console.WriteLine(car.Name);
            }

            Console.WriteLine("****Finding the top 10 most fuel efficent cars with extension method syntax****");
            var query =
                cars.OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name);

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

            Console.WriteLine("****Finding the top 10 most fuel efficent cars with query method syntax****");
            query =
                from car in cars
                orderby car.Combined descending, car.Name ascending
            select car;

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

            Console.WriteLine("****Finding the top 10 most fuel efficent cars by manufacturer with extension method syntax****");
            query =
                cars.Where(c => c.Manufacturer == "BMW" && c.Year == 2016)
                .OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name);

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

            Console.WriteLine("****Finding the top 10 most fuel efficent cars by manufacturer and year with query method syntax****");
            query =
                from car in cars
                where car.Manufacturer == "BMW" && car.Year == 2016
                orderby car.Combined descending, car.Name ascending
            select car;

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

            Console.WriteLine("****Finding the best most fuel efficent car by manufacturer and year with extension method syntax****");
            var selectedCar =
                cars.Where(c => c.Manufacturer == "BMW" && c.Year == 2016)
                .OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name).FirstOrDefault();

            if (selectedCar != null)
            {
                Console.WriteLine($"{selectedCar.Name}: {selectedCar.Combined}");
            }

            Console.WriteLine("****Is there any car manufactured by Ford****");
            var isCarManufacturedByFord = cars.Any(cars => cars.Manufacturer == "Ford");

            Console.WriteLine(isCarManufacturedByFord);

            Console.WriteLine("****Is there every car manufactured by Ford****");
            var isEveryCarManufacturedByFord = cars.All(cars => cars.Manufacturer == "Ford");

            Console.WriteLine(isEveryCarManufacturedByFord);

            Console.WriteLine("****Is there any car manufactured by BMW and produced in 2016****");
            var isCarBMWProducedIn2016 = cars.Contains(selectedCar);

            Console.WriteLine(isCarBMWProducedIn2016);

            Console.WriteLine("****Show 15 car names with custom extension method ToCar****");
            cars = ProcessMethods.ProcessCarFileByExtensionMethodCalledToCar("fuel.csv");

            foreach (var car in cars.Take(15))
            {
                Console.WriteLine(car.Name);
            }

            Console.WriteLine("****Finding the top 10 most fuel efficent cars by manufacturer and year by projection with anonymous type****");
            var queryFromProjection =
                from car in cars
                where car.Manufacturer == "BMW" && car.Year == 2016
                orderby car.Combined descending, car.Name ascending
                select new
            {
                car.Manufacturer,
                car.Name,
                car.Combined
            };

            foreach (var car in queryFromProjection.Take(10))
            {
                Console.WriteLine($"{car.Manufacturer} {car.Name}: {car.Combined}");
            }

            Console.WriteLine("****Show 15 car names by projection with anonymous type****");
            queryFromProjection =
                cars.Select(c => new
            {
                c.Manufacturer,
                c.Name,
                c.Combined
            });

            foreach (var car in query.Take(15))
            {
                Console.WriteLine($"{car.Manufacturer} {car.Name}: {car.Combined}");
            }

            Console.WriteLine("****Show 2 car names and split it by characters by two foreach loops****");
            var queryTwoLoops = cars.Select(c => c.Name);

            foreach (var name in queryTwoLoops.Take(2))
            {
                foreach (var character in name)
                {
                    Console.WriteLine(character);
                }
            }

            Console.WriteLine("****Show 2 car names and split it by characters by flat data with SelectMany****");
            var querySelectMany = cars.Take(2).SelectMany(c => c.Name);

            foreach (var character in querySelectMany)
            {
                Console.WriteLine(character);
            }
        }
示例#3
0
        public static void LinqOnCarsAndManufacturers()
        {
            Console.WriteLine("****LINQ ON MANUFACTURERS****");

            Console.WriteLine("****Finding the top 10 most fuel efficent cars and thier headquaters with join data with query method syntax****");
            var cars          = ProcessMethods.ProcessCarFileByExtensionMethod("fuel.csv");
            var manufacturers = ProcessMethods.ProcessManufacturersFileByExtensionMethod("manufacturers.csv");

            var query =
                from car in cars
                join manufacturer in manufacturers
                on car.Manufacturer equals manufacturer.Name
                orderby car.Combined descending, car.Name ascending
                select new
            {
                manufacturer.Name,
                CarName = car.Name,
                manufacturer.Headquater,
                car.Combined
            };

            foreach (var item in query.Take(10))
            {
                Console.WriteLine($"{item.Name} {item.CarName} {item.Headquater} : {item.Combined} ");
            }

            Console.WriteLine("****Finding the top 10 most fuel efficent cars and thier headquaters with join data with extension method syntax****");
            query =
                cars.Join(manufacturers,
                          c => c.Manufacturer,
                          m => m.Name,
                          (c, m) => new
            {
                m.Name,
                CarName = c.Name,
                m.Headquater,
                c.Combined
            }).OrderByDescending(c => c.Combined).ThenBy(c => c.CarName);

            foreach (var item in query.Take(10))
            {
                Console.WriteLine($"{item.Name} {item.CarName} {item.Headquater} : {item.Combined} ");
            }

            Console.WriteLine("****Finding the top 10 most fuel efficent cars and thier headquaters with join with composite key with query method syntax****");
            query =
                from car in cars
                join manufacturer in manufacturers
                on new { car.Manufacturer, car.Year }
            equals
            new { Manufacturer = manufacturer.Name, manufacturer.Year }
            orderby car.Combined descending, car.Name ascending
                                 select new
            {
                manufacturer.Name,
                CarName = car.Name,
                manufacturer.Headquater,
                car.Combined
            };

            foreach (var item in query.Take(10))
            {
                Console.WriteLine($"{item.Name} {item.CarName} {item.Headquater} : {item.Combined} ");
            }

            Console.WriteLine("****Finding the top 10 most fuel efficent cars and thier headquaters with join with composite key with extension method syntax****");
            query =
                cars.Join(manufacturers,
                          c => new { c.Manufacturer, c.Year },
                          m => new { Manufacturer = m.Name, m.Year }, (c, m) => new
            {
                m.Name,
                CarName = c.Name,
                m.Headquater,
                c.Combined
            }).OrderByDescending(c => c.Combined).ThenBy(c => c.CarName);

            foreach (var item in query.Take(10))
            {
                Console.WriteLine($"{item.Name} {item.CarName} {item.Headquater} : {item.Combined} ");
            }

            Console.WriteLine("****List cars count by manufacturers using group with query method syntax****");
            var groupedCars = from car in cars
                              group car by car.Manufacturer.ToUpper()
                              into manufacturer
                              orderby manufacturer.Key
                              select manufacturer;

            foreach (var group in groupedCars)
            {
                Console.WriteLine($"{ group.Key } produced { group.Count() } cars");
            }

            Console.WriteLine("****List cars count by manufacturers using group with extension method syntax****");
            groupedCars = cars.GroupBy(c => c.Manufacturer.ToUpper())
                          .OrderBy(g => g.Key);

            foreach (var group in groupedCars)
            {
                Console.WriteLine($"{ group.Key } produced { group.Count() } cars");
            }

            Console.WriteLine("****Finding the top 2 most fuel efficent cars grouped by manufacturers using group****");

            foreach (var group in groupedCars)
            {
                Console.WriteLine(group.Key);
                foreach (var car in group.OrderByDescending(c => c.Combined).Take(2))
                {
                    Console.WriteLine($"\t{ car.Name } : { car.Combined }");
                }
            }

            Console.WriteLine("****Finding the top 2 most fuel efficent cars grouped by manufacturers using group join with query method syntax****");
            var groupedJoinByOrderedByManufacturersName = from manufacturer in manufacturers
                                                          join car in cars
                                                          on manufacturer.Name.ToUpper() equals car.Manufacturer.ToUpper()
                                                          into carGroup
                                                          orderby manufacturer.Name
                                                          select new
            {
                Manufacturer = manufacturer,
                Car          = carGroup
            };

            foreach (var group in groupedJoinByOrderedByManufacturersName)
            {
                Console.WriteLine($"{ group.Manufacturer.Name }: {group.Manufacturer.Headquater }");
                foreach (var car in group.Car.Take(2).OrderBy(x => x.Combined))
                {
                    Console.WriteLine($"\t { car.Name } { car.Combined }");
                }
            }

            Console.WriteLine("****Finding the top 2 most fuel efficent cars grouped by manufacturers using group join with extension method syntax****");
            groupedJoinByOrderedByManufacturersName = manufacturers.GroupJoin(cars,
                                                                              m => m.Name.ToUpper(),
                                                                              c => c.Manufacturer.ToUpper(),
                                                                              (m, g) => new
            {
                Manufacturer = m,
                Car          = g
            })
                                                      .OrderBy(m => m.Manufacturer.Name);

            foreach (var group in groupedJoinByOrderedByManufacturersName)
            {
                Console.WriteLine($"{ group.Manufacturer.Name }: { group.Manufacturer.Headquater }");
                foreach (var car in group.Car.OrderBy(g => g.Combined).Take(2))
                {
                    Console.WriteLine($"\t { car.Name } { car.Combined }");
                }
            }

            Console.WriteLine("****Finding the top 3 most fuel efficent cars grouped by countries using group join with query method syntax****");
            var groupedJoinByManufacturersOrderedByCountry = from manufacturer in manufacturers
                                                             join car in cars
                                                             on manufacturer.Name.ToUpper() equals car.Manufacturer.ToUpper()
                                                             into carGroup
                                                             orderby manufacturer.Name
                                                             select new
            {
                Manufacturer = manufacturer,
                Car          = carGroup
            }
            into result
            orderby result.Manufacturer.Headquater
            group result by result.Manufacturer.Headquater;

            foreach (var group in groupedJoinByManufacturersOrderedByCountry)
            {
                Console.WriteLine($"{ group.Key }");
                foreach (var car in group.SelectMany(x => x.Car).OrderByDescending(x => x.Combined).Take(3))
                {
                    Console.WriteLine($"\t{ car.Name } {car.Combined}");
                }
            }

            Console.WriteLine("****Finding the top 3 most fuel efficent cars grouped by countries using group join with extension method syntax****");
            groupedJoinByManufacturersOrderedByCountry = manufacturers.GroupJoin(cars,
                                                                                 m => m.Name.ToUpper(),
                                                                                 c => c.Manufacturer.ToUpper(),
                                                                                 (m, g) => new
            {
                Manufacturer = m,
                Car          = g
            })
                                                         .GroupBy(m => m.Manufacturer.Headquater)
                                                         .OrderBy(m => m.Key);

            foreach (var group in groupedJoinByManufacturersOrderedByCountry)
            {
                Console.WriteLine($"{ group.Key }");
                foreach (var car in group.SelectMany(x => x.Car).OrderByDescending(x => x.Combined).Take(3))
                {
                    Console.WriteLine($"\t{ car.Name } {car.Combined}");
                }
            }

            Console.WriteLine("****Find the manufacturer with most fuel efficent car with query method syntax****");
            var aggregatedCars = from car in cars
                                 group car by car.Manufacturer
                                 into carGroup
                                 select new
            {
                Name = carGroup.Key.ToUpper(),
                Max  = carGroup.Max(c => c.Combined),
                Min  = carGroup.Min(c => c.Combined),
                Avg  = carGroup.Average(c => c.Combined)
            }
            into result
            orderby result.Max descending
            select result;

            foreach (var result in aggregatedCars)
            {
                Console.WriteLine($"{ result.Name}");
                Console.WriteLine($"\tMax: { result.Max }");
                Console.WriteLine($"\tMin: { result.Min }");
                Console.WriteLine($"\tAvg: { result.Avg }");
            }

            Console.WriteLine("****Find the manufacturer with most fuel efficent car with extension method syntax****");
            aggregatedCars = cars.GroupBy(c => c.Manufacturer)
                             .Select(g =>
            {
                return(new
                {
                    Name = g.Key.ToUpper(),
                    Max = g.Max(c => c.Combined),
                    Min = g.Min(c => c.Combined),
                    Avg = g.Average(c => c.Combined)
                });
            })
                             .OrderByDescending(g => g.Max);

            foreach (var result in aggregatedCars)
            {
                Console.WriteLine($"{ result.Name}");
                Console.WriteLine($"\tMax: { result.Max }");
                Console.WriteLine($"\tMin: { result.Min }");
                Console.WriteLine($"\tAvg: { result.Avg }");
            }

            Console.WriteLine("****Find the manufacturer with most fuel efficent car with aggregating extension method syntax****");
            var sasggregatedCars = cars.GroupBy(c => c.Manufacturer)
                                   .Select(g =>
            {
                var results = g.Aggregate(new CarStatistics(),
                                          (acc, c) => acc.Accumulate(c),
                                          acc => acc.Compute());
                return(new
                {
                    Name = g.Key.ToUpper(),
                    Max = results.Average,
                    Min = results.Min,
                    Avg = results.Max
                });
            })
                                   .OrderByDescending(g => g.Max);

            foreach (var result in aggregatedCars)
            {
                Console.WriteLine($"{ result.Name}");
                Console.WriteLine($"\tMax: { result.Max }");
                Console.WriteLine($"\tMin: { result.Min }");
                Console.WriteLine($"\tAvg: { result.Avg }");
            }
        }