public static void Video6_3_Joining_Data_With_Query_Syntax_Start() { Console.WriteLine(""); Console.WriteLine("Video6_3_Joining_Data_With_Query_Syntax"); Console.WriteLine("-------------------------------------"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufactuers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Query Syntax herunder var queryQuerySyntax = from car in cars join manufacturer in manufactuers on car.Manufacturer equals manufacturer.Name orderby car.Combined descending, car.Name ascending select new { manufacturer.Headquarters, car.Name, car.Combined }; foreach (var car in queryQuerySyntax.Take(10)) { Console.WriteLine($"{car.Headquarters} : {car.Name} : {car.Combined}"); } }
public static void Video6_5_Creating_A_Join_With_A_Composition_Start() { Console.WriteLine(""); Console.WriteLine("Video6_5_Creating_A_Join_With_A_Composition"); Console.WriteLine("-----------------------------------------"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Query Syntax herunder // Når vi bruger Query Syntax, er der en "tradition" for, at vi // bestræber os på at gøre tingene så læsbare, som vi kan. Det kan // ses herunder, hvor vi bruger navnene car og manufacurer. Ví kunne // have valgt at bruge én bogstavs navne som f.eks. c (i stedet for car) // og m (i stedet for manufacturer). // Når vi bruger Extension Method Syntax er der omvendt en "´tradition" for, // at vi anvender én bogstavs navne, når vi kommer omkring Lambda Expressions // og vi skal navngive/specificere indkommende parametre til Lambda // Expressions. Dette kan vi se, når vi kommer til Extension Method Syntax // i det efterfølgende. // I de foregående eksempler har der været brugt både // én bogstavs navne og mere sigende og længere navne, når vi har brugt // navngivning for Lambda Expression indkommende prrametre i forbindelse // med Extension Method Syntax. var queryQuerySyntax = from car in cars join manufacturer in manufacturers on new { car.Manufacturer, car.Year } equals new { Manufacturer = manufacturer.Name, manufacturer.Year } // Navnet skal være det samme i Car.Manufacturer og manufacturer.name // Derfor er det nødvendig med sætningen : Manufacturer = manufacturer.Name !!! orderby car.Combined descending, car.Name ascending select new { manufacturer.Headquarters, car.Name, car.Combined }; Console.WriteLine("Query Syntax"); Console.WriteLine(""); foreach (var car in queryQuerySyntax.Take(10)) { Console.WriteLine($"{car.Headquarters} : {car.Name} : {car.Combined}"); } Console.WriteLine(""); Console.WriteLine("Extension Method Syntax"); Console.WriteLine(""); // Extension Method Syntax herunder. // Som nævnt tidligere i dette dokument, bruger vi her én bogstavs // navngivning for indkommende Lambda Expression parametre i // forbindelse med Extension Method Syntax. Og i alle efterfølgende // eksmepler vil denne én bogstavs navngivning blive anvendt. // I forbindelse med denne én bogstavs navngivning har jeg valgt at // benytte første bogstav i det navn, som vi forkorter efter. // Det vil sige c for for car og m for manufacturer og så fremdeles. // Denne navgivnings konvention er også den, der gennrelt amvendes, // nåt vi snakker om Lambda Expression og indkommende parametre. // efterfølgende Lambda Expressions var queryMethodSyntax = cars.Join(manufacturers, c => new { c.Manufacturer, c.Year }, m => new { Manufacturer = m.Name, m.Year }, // Navnet skal være det samme i C.Manufacturer og m.name // Derfor er det nødvendig med sætningen : Manufacturer = m.Name !!! (c, m) => new { m.Headquarters, c.Name, c.Combined }) .OrderByDescending(c => c.Combined) .ThenBy(c => c.Name); foreach (var car in queryMethodSyntax.Take(10)) { Console.WriteLine($"{car.Headquarters} : {car.Name} : {car.Combined}"); } }
public static void Video6_4_Joining_Data_Using_Extension_Method_Syntax_Start() { Console.WriteLine(""); Console.WriteLine("Video6_4_Joining_Data_Using_Extension_Method_Syntax"); Console.WriteLine("------------------------------------------------"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Extension Method Syntax herunder // Navngivningen i eksemplet herunder er den man tiest støder på, når man // arbejder med Lambda Expression. // Ved alle indkommende parametre til et Lambda Expression benytter man // sig af én bogstavs notation. For at gøre det lettere at læse koden // benytter man ofte af et bogstav, der er første bogtsva i det, man // arbejder med. F.eks c for car og m for manufacturer. var queryMethodSyntaxSingleLetterNaming = cars.Join(manufacturers, c => c.Manufacturer, m => m.Name, (c, m) => new { m.Headquarters, c.Name, c.Combined }) // Her bliver de 2 objekter cars og manufacturers samlet i ét objekt .OrderByDescending(c => c.Combined) .ThenBy(c => c.Name); foreach (var car in queryMethodSyntaxSingleLetterNaming.Take(10)) { Console.WriteLine($"{car.Headquarters} : {car.Name} : {car.Combined}"); } Console.WriteLine(""); // Navngivningen i eksemplet herunder støder man ikke på så tit, når man // arbejder med Lambda Expression. Men den er måske lettere at læse // end én bogstavs navngivningen fra eksemplet herover. // Som altid indefor progtrammeringens verden er det et spørgsmål // om smag og behag. Og der er ikke noget, der er mere rigtigt end andet. var queryMethodSyntaxMoreReadableNaming = cars.Join(manufacturers, car => car.Manufacturer, manufacturer => manufacturer.Name, (car, manufacturer) => new { manufacturer.Headquarters, car.Name, car.Combined }) // Her bliver de 2 objekter cars og manufacturers samlet i ét objekt .OrderByDescending(car => car.Combined) .ThenBy(car => car.Name); foreach (var car in queryMethodSyntaxMoreReadableNaming.Take(10)) { Console.WriteLine($"{car.Headquarters} : {car.Name} : {car.Combined}"); } Console.WriteLine(""); var queryMethodSyntax1 = cars.Join(manufacturers, c => c.Manufacturer, m => m.Name, (c, m) => new { Car = c, Manufacturer = m }) .OrderByDescending(c => c.Car.Combined) .ThenBy(c => c.Car.Name); foreach (var car in queryMethodSyntax1.Take(10)) { Console.WriteLine($"{car.Manufacturer.Headquarters} : {car.Car.Manufacturer} : {car.Car.Name} : {car.Car.Combined}"); } Console.WriteLine(""); var queryMethodSyntax2 = cars.Join(manufacturers, c => c.Manufacturer, m => m.Name, (c, m) => new { Car = c, Manufacturer = m }) .OrderByDescending(c => c.Car.Combined) .ThenBy(c => c.Car.Name) .Select(c => new { c.Manufacturer.Headquarters, c.Car.Name, c.Car.Combined }); foreach (var car in queryMethodSyntax1.Take(10)) { Console.WriteLine($"{car.Manufacturer.Headquarters} : {car.Car.Name} : {car.Car.Combined}"); } // Der kan ikke bruges Include her, som når vi anvender EntityFramework // op imod f.eks. en database. // Så Include, ThenInclude o.s.ver altså funktionalitet, som Entity // Framework bygger ovenpå Linq. }
public static void Video6_9_Aggreating_Data_Start() { Console.WriteLine(""); Console.WriteLine("Video6_9_Aggreating_Data"); Console.WriteLine("----------------------"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Query Syntax herunder var queryQuerySyntax = from car in cars group car by car.Manufacturer into carGroup select new { // Vi laver en projektion her. Name = carGroup.Key, Max = carGroup.Max(c => c.Combined), Min = carGroup.Min(c => c.Combined), Avg = carGroup.Average(c => c.Combined), NumberOfCars = carGroup.Count() }; Console.WriteLine("Query Syntax"); Console.WriteLine(""); foreach (var result in queryQuerySyntax) { Console.WriteLine($"{result.Name} : "); Console.WriteLine($"\t Antal {result.Name} biler : {result.NumberOfCars}"); Console.WriteLine($"\t Max : {result.Max}"); Console.WriteLine($"\t Min : {result.Min}"); Console.WriteLine($"\t Avg : {result.Avg}"); Console.WriteLine(""); } var queryQuerySyntaxOrdered = from car in cars group car by car.Manufacturer into carGroup select new { // Vi laver en projektion her. Name = carGroup.Key, Max = carGroup.Max(c => c.Combined), Min = carGroup.Min(c => c.Combined), Avg = carGroup.Average(c => c.Combined), NumberOfCars = carGroup.Count() } into result orderby result.Max descending select result; Console.WriteLine(""); Console.WriteLine("Ordered by most fuel efficient car"); Console.WriteLine(""); foreach (var result in queryQuerySyntaxOrdered) { Console.WriteLine($"{result.Name} : "); Console.WriteLine($"\t Antal {result.Name} biler : {result.NumberOfCars}"); Console.WriteLine($"\t Max : {result.Max}"); Console.WriteLine($"\t Min : {result.Min}"); Console.WriteLine($"\t Avg : {result.Avg}"); Console.WriteLine(""); } }
public static void Video6_8_Challenge_Answer_Group_By_Country_start() { Console.WriteLine(""); Console.WriteLine("Video6_8_Challenge_Answer_Group_By_Country"); Console.WriteLine("----------------------------------------"); Console.WriteLine(""); Console.WriteLine("Nu sorteres efter land og derefter efter 3 mest brændstof økonomiske biler pr land"); Console.WriteLine(""); Console.WriteLine("Der anvendes joining - ordering - grouping - flattening => (SelectMany)"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Query Syntax herunder Console.WriteLine(""); Console.WriteLine("Query Syntax"); Console.WriteLine(""); var queryQuerySyntax = from manufacturer in manufacturers join car in cars on manufacturer.Name equals car.Manufacturer into carGroup select new { Manufacturer = manufacturer, Cars = carGroup } into result orderby result.Manufacturer.Headquarters group result by result.Manufacturer.Headquarters; foreach (var group in queryQuerySyntax) { Console.WriteLine($"{group.Key} : "); foreach (var car in group.SelectMany(g => g.Cars). OrderByDescending(c => c.Combined). Take(3)) { Console.WriteLine($"\t{car.Manufacturer} : {car.Name} : {car.Combined}"); } Console.WriteLine(""); } // Extension Method Syntax herunder. Console.WriteLine(""); Console.WriteLine("Extension Method Syntax"); Console.WriteLine(""); var queryMethodSyntax = manufacturers.GroupJoin(cars, m => m.Name, c => c.Manufacturer, (m, g) => new { Manufacturer = m, Cars = g }) .OrderBy(m => m.Manufacturer.Headquarters) .GroupBy(m => m.Manufacturer.Headquarters); foreach (var group in queryMethodSyntax) { Console.WriteLine($"{group.Key} : "); foreach (var car in group.SelectMany(g => g.Cars). OrderByDescending(c => c.Combined). Take(3)) { Console.WriteLine($"\t{car.Manufacturer} : {car.Name} : {car.Combined}"); } Console.WriteLine(""); } }
public static void Video6_10_Efficient_Aggregation_With_Extended_Method_Start() { Console.WriteLine(""); Console.WriteLine("Video6_10_Efficient_Aggregation_With_Extended_Method"); Console.WriteLine("----------------------"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Extension Method Syntax herunder. Console.WriteLine("Extension Method Syntax"); Console.WriteLine(""); Console.WriteLine(""); var queryMethodSyntaxOwnFunction_Static = cars.GroupBy(c => c.Manufacturer) .Select(g => { var results = CarStatistics.CalculateCarsStatistics_Static(g); return(new { Name = g.Key, Max = results.Max, Min = results.Min, Avg = results.Average, NumberOfCars = results.NumberOfCars }); }) .OrderByDescending(r => r.Max); Console.WriteLine("Udskrift fra queryMethodSyntaxOwnFunction_Static"); Console.WriteLine(""); foreach (var result in queryMethodSyntaxOwnFunction_Static) { Console.WriteLine($"{result.Name} : "); Console.WriteLine($"\t Antal {result.Name} biler : {result.NumberOfCars}"); Console.WriteLine($"\t Max : {result.Max}"); Console.WriteLine($"\t Min : {result.Min}"); Console.WriteLine($"\t Avg : {result.Avg}"); Console.WriteLine(""); } Console.WriteLine(""); Console.WriteLine(""); var queryMethodSyntaxOwnFunction_Object = cars.GroupBy(c => c.Manufacturer) .Select(g => { CarStatistics carStatistics_Object = new CarStatistics(); var results = carStatistics_Object.CalculateCarsStatistics_Object(g); return(new { Name = g.Key, Max = results.Max, Min = results.Min, Avg = results.Average, NumberOfCars = results.NumberOfCars }); }) .OrderByDescending(r => r.Max); Console.WriteLine("Udskrift fra queryMethodSyntaxOwnFunction_Object"); Console.WriteLine(""); foreach (var result in queryMethodSyntaxOwnFunction_Object) { Console.WriteLine($"{result.Name} : "); Console.WriteLine($"\t Antal {result.Name} biler : {result.NumberOfCars}"); Console.WriteLine($"\t Max : {result.Max}"); Console.WriteLine($"\t Min : {result.Min}"); Console.WriteLine($"\t Avg : {result.Avg}"); Console.WriteLine(""); } Console.WriteLine(""); Console.WriteLine(""); var queryMethodSyntaxOwnFunction_ExtensionMethod = cars.GroupBy(c => c.Manufacturer) .Select(g => { var results = g.CalculateCarsStatistics(); return(new { Name = g.Key, Max = results.Max, Min = results.Min, Avg = results.Average, NumberOfCars = results.NumberOfCars }); }) .OrderByDescending(r => r.Max); Console.WriteLine("Udskrift fra queryMethodSyntaxOwnFunction_Static"); Console.WriteLine(""); foreach (var result in queryMethodSyntaxOwnFunction_ExtensionMethod) { Console.WriteLine($"{result.Name} : "); Console.WriteLine($"\t Antal {result.Name} biler : {result.NumberOfCars}"); Console.WriteLine($"\t Max : {result.Max}"); Console.WriteLine($"\t Min : {result.Min}"); Console.WriteLine($"\t Avg : {result.Avg}"); Console.WriteLine(""); } Console.WriteLine(""); Console.WriteLine(""); var queryMethodSyntax_From_Scott_Allen_PLuralsight_Video = 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, Max = results.Max, Min = results.Min, Avg = results.Average, NumberOfCars = results.NumberOfCars }); }) .OrderByDescending(r => r.Max); // Ved at bruge syntaksen/ metoden herover undgår vi at skulle // løbe igennem alle Cars 3 gange for at finde henholdsvis Max, Min // og Average værdier. Console.WriteLine("Udskrift fra queryMethodSyntax_From_Scott_Allen_PLuralsight_Video"); Console.WriteLine(""); foreach (var result in queryMethodSyntax_From_Scott_Allen_PLuralsight_Video) { Console.WriteLine($"{result.Name} : "); Console.WriteLine($"\t Antal {result.Name} biler : {result.NumberOfCars}"); Console.WriteLine($"\t Max : {result.Max}"); Console.WriteLine($"\t Min : {result.Min}"); Console.WriteLine($"\t Avg : {result.Avg}"); Console.WriteLine(""); } }
public static void Video6_6_Grouping_Data_Start() { Console.WriteLine(""); Console.WriteLine("Video6_6_Grouping_Data"); Console.WriteLine("--------------------"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Query Syntax herunder var queryQuerySyntax = from car in cars group car by car.Manufacturer; Console.WriteLine("Query Syntax"); Console.WriteLine(""); foreach (var group in queryQuerySyntax) { Console.WriteLine($"{group.Key}har {group.Count()} cars"); // group by giver et Key felt !!! } Console.WriteLine(""); foreach (var group in queryQuerySyntax) { Console.WriteLine(group.Key); foreach (var car in group.OrderByDescending(c => c.Combined).Take(2)) { Console.WriteLine($"\t{car.Name} : {car.Combined}"); } Console.WriteLine(""); } var queryQuerySyntax1 = from car in cars group car by car.Manufacturer.ToUpper() into manufacturer orderby manufacturer.Key select manufacturer; Console.WriteLine(""); foreach (var group in queryQuerySyntax1) { Console.WriteLine(group.Key); foreach (var car in group.OrderByDescending(c => c.Combined).Take(2)) { Console.WriteLine($"\t{car.Name} : {car.Combined}"); } Console.WriteLine(""); } // Extension Method Syntax herunder. Console.WriteLine(""); Console.WriteLine("Extension Method Syntax"); Console.WriteLine(""); var queryMethodSyntax = cars.GroupBy(c => c.Manufacturer.ToUpper()) .OrderBy(g => g.Key) .Select(c => c); // Som sædvanlig ikke nødvendig med .Select for et Expresion Method // Syntax udtryk. Men man kan tage det med, hvis man synes. foreach (var group in queryMethodSyntax) { Console.WriteLine(group.Key); foreach (var car in group.OrderByDescending(c => c.Combined).Take(2)) { Console.WriteLine($"\t{car.Name} : {car.Combined}"); } Console.WriteLine(""); } }
public static void Video6_7_Using_A_GroupJoin_For_Hierarchy_Start() { Console.WriteLine(""); Console.WriteLine("Video6_7_Using_A_GroupJoin_For_Hierarchy"); Console.WriteLine("--------------------------------------"); Console.WriteLine(""); Console.WriteLine("Nu lande informationer med for Headquarters"); Console.WriteLine(""); var cars = ProcessDataFromFiles.ProcessCar("fuel.csv"); var manufacturers = ProcessDataFromFiles.ProcessManufacturers("manufacturers.csv"); // Query Syntax herunder var queryQuerySyntax = from manufacturer in manufacturers join car in cars on manufacturer.Name equals car.Manufacturer into carGroup orderby manufacturer.Name select new { Manufacturer = manufacturer, Cars = carGroup }; Console.WriteLine("Query Syntax"); Console.WriteLine(""); foreach (var group in queryQuerySyntax) { Console.WriteLine($"{group.Manufacturer.Name} : {group.Manufacturer.Headquarters}"); foreach (var car in group.Cars.OrderByDescending(c => c.Combined).Take(2)) { Console.WriteLine($"\t{car.Name} : {car.Combined}"); } Console.WriteLine(""); } // Extension Method Syntax herunder. Console.WriteLine(""); Console.WriteLine("Extension Method Syntax"); Console.WriteLine(""); var queryMethodSyntax = manufacturers.GroupJoin(cars, m => m.Name, c => c.Manufacturer, (m, g) => new { Manufacturer = m, Cars = g }) .OrderBy(m => m.Manufacturer.Name); foreach (var group in queryMethodSyntax) { Console.WriteLine($"{group.Manufacturer.Name} : {group.Manufacturer.Headquarters}"); foreach (var car in group.Cars.OrderByDescending(c => c.Combined).Take(2)) { Console.WriteLine($"\t{car.Name} : {car.Combined}"); } Console.WriteLine(""); } }