private static void Main(string[] args) { string filePath = @"C:\...\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); List <Country> countries = reader.ReadAllCountries(); Country lilliput = new Country("Lilliput", "LIL", "Somewhere", 2_000_000); int lilliputIndex = countries.FindIndex(x => x.Population < 2_000_000); countries.Insert(lilliputIndex, lilliput); countries.RemoveAt(lilliputIndex); //foreach (Country country in countries) //foreach (Country country in countries.Take(10)) //foreach (Country country in countries.OrderBy(x=>x.Name).Take(1000)) //foreach (Country country in countries.Where(x => !x.Name.Contains(',')).Take(20)) var filteredCountries = countries.Where(x => !x.Name.Contains(',')); //.Take(20); var filteredCountries2 = from country in countries where !country.Name.Contains(',') select country; foreach (Country country in filteredCountries2) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } for (int i = 12; i <= 14; i++) { Console.WriteLine($"index: {i}: {countries[i].Name}"); } Console.WriteLine($"{countries.Count} countries"); }
static void Main(string[] args) { string filePath = @"G:\G_Work\Pluralsight\Courses\Beginning Effective Collections\Code\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); Dictionary <string, List <Country> > countries = reader.ReadAllCountries(); foreach (string region in countries.Keys) { Console.WriteLine(region); } Console.Write("Which of the above regions do you want? "); string chosenRegion = Console.ReadLine(); if (countries.ContainsKey(chosenRegion)) { // display 10 highest population countries in the selected region foreach (Country country in countries[chosenRegion].Take(10)) { Console.WriteLine($"{PopulationFormatter.FormatPopulation (country.Population).PadLeft(15)}: {country.Name}"); } } else { Console.WriteLine("That is not a valid region"); } }
static void Main(string[] args) { //string filePath = @"C:\Users\david.hernandez\source\repos\Beginning Collections\m08 coll-coll\CountriesByRegion\Pop by Largest Final.csv"; string filePath = "Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); Dictionary <string, List <Country> > countries = reader.ReadAllCountries(); Console.WriteLine("Which of the above regions do you want? "); int count = 1; var ListaKeys = new List <string>(); foreach (string region in countries.Keys) { Console.WriteLine($" {count++} {region}"); ListaKeys.Add(region); } bool done = false; do { Console.WriteLine("Digite el numero de la region o si quiere salir q"); string input = Console.ReadLine(); bool regionCorrecta = int.TryParse(input, out int regionElegida); if (regionCorrecta && regionElegida <= 6 && regionElegida > 0) { regionElegida -= 1; if (countries.ContainsKey(ListaKeys[regionElegida])) { foreach (Country country in countries[ListaKeys[regionElegida]].Take(10)) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } } } else if (input == "q") { done = true; break; } else { Console.WriteLine("El valor digitado no es valido"); } } while (!done); //if (countries.ContainsKey(chosenRegion)) //{ // // display 10 highest population countries in the selected region // foreach (Country country in countries[chosenRegion].Take(10)) // Console.WriteLine($"{PopulationFormatter.FormatPopulation (country.Population).PadLeft(15)}: {country.Name}"); //} //else // Console.WriteLine("That is not a valid region"); }
static void Main(string[] args) { string filePath = @"E:\Capgemini\Azure Training\C# Collections\LINQ\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); List <Country> countries = reader.ReadAllCountries(); var filteredCountries = countries.Where(x => !x.Name.Contains(',')).Take(20); //Link Query Syntax var filteredCountries2 = from country in countries where !country.Name.Contains(',') select country; // listing the first 20 countries without commas in their names foreach (Country country in filteredCountries) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } Console.WriteLine(); // listing the 10 highest population countries in alphabetical order. // Reverse Where() and Take() to see the impact of swapping chaining order round /*foreach (Country country in countries.Take(10).OrderBy(x=>x.Name)) * { * Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); * }*/ }
static void Main(string[] args) { string filePath = @"C:\Users\matma\Desktop\PopbyLargestFinal.csv"; CsvReader reader = new CsvReader(filePath); List <Country> countries = reader.ReadAllCountries(); reader.RemoveCommaCountries(countries); /*Country lilliput = new Country("Lilliput", "LIL", "Somewhere", 2_000_000); * int lilliputIndex = countries.FindIndex(x=> x.Population < 2_000_000); * countries.Insert(lilliputIndex, lilliput); * // countries.RemoveAt(lilliputIndex); */ Console.Write("Enter no. of countries to display> "); bool inputIsInt = int.TryParse(Console.ReadLine(), out int userInput); if (!inputIsInt || userInput <= 0) { Console.WriteLine("You must type in +ve integer. Exiting"); return; } int maxToDisplay = userInput; //foreach (Country country in countries) // wyświetlanie danych za pomocą pętli foreach for (int i = 0; i < countries.Count; i++) // wyświetlanie danych od najwyższej wartości { if (i > 0 && (i % maxToDisplay == 0)) { Console.WriteLine("Hit return to continue, anything else to quit>"); if (Console.ReadLine() != "") { break; } } Country country = countries[i]; //wczytuje wszystkie dane z pliku za pomocą pętli for Console.WriteLine($"{i+1}: {PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } //Console.WriteLine($"{countries.Count} countries"); Console.ReadKey(); }
static void Main(string[] args) { string filePath = @"C:\Users\Rui Reis\program\learning\dotnet\pluralsight\Robinson_Beginning_CSharp_Collections\code\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); List <Country> countries = reader.ReadAllCountries(); var filteredCountries = countries.Where(x => !x.Name.Contains(','));//.ToList().GetRange(0, 20); var filteredCountries2 = from country in countries where !country.Name.Contains(',') select country; //foreach (Country country in countries.GetRange(0, 20)) foreach (Country country in filteredCountries2) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: { country.Name}"); } //Console.WriteLine($"{countries.Count} countries."); }
static void Main(string[] args) { string filePath = @"G:\G_Work\Pluralsight\Courses\Beginning Effective Collections\Code\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); List <Country> countries = reader.ReadAllCountries(); // This is the code that inserts and then subsequently removes Lilliput. // Comment out the RemoveAt to see the list with Lilliput in it. Country lilliput = new Country("Lilliput", "LIL", "Somewhere", 2_000_000); int lilliputIndex = countries.FindIndex(x => x.Population < 2_000_000); countries.Insert(lilliputIndex, lilliput); countries.RemoveAt(lilliputIndex); foreach (Country country in countries) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } Console.WriteLine($"{countries.Count} countries"); }
static void Main(string[] args) { string filePath = @"G:\G_Work\Pluralsight\Courses\Beginning Effective Collections\Code\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); List <Country> countries = reader.ReadAllCountries(); // listing the first 20 countries without commas in their names foreach (Country country in countries.Where(x => !x.Name.Contains(',')).Take(20)) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } Console.WriteLine(); // listing the 10 highest population countries in alphabetical order. // Reverse Where() and Take() to see the impact of swapping chaining order round foreach (Country country in countries.Take(10).OrderBy(x => x.Name)) { Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); } }
static void Main(string[] args) { string filePath = @"C:\Users\tarep\Documents\PluralSight\BeginningCSharpCollections_Pluralsight\Pop by Largest Final.csv"; CsvReader reader = new CsvReader(filePath); //creating a list of Country instances List <Country> countries = reader.ReadAllCountries(); //removing countries with ',' in the name reader.RemoveCommaCountries(countries); //adding a new country to the list var wakanda = new Country("Wakanda", "WAK", "Africa", 6_000_000); //using a lambda expression to find wakanda position / index int wakandaIndex = countries.FindIndex(_ => _.Population < 6_000_000); //insert the instance in an especific index //it can prejudicate the software performance, cause all the positions after the insertion will change in the memory countries.Insert(wakandaIndex, wakanda); //you can remove using the index too //countries.RemoveAt(wakandaIndex); //enumerate all countries at the file /* * foreach (Country country in countries) * { * Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}"); * } */ //enumerate only the quantity especified by user Console.WriteLine("Enter the number of countries you wanna display: "); //read the user input and verifify if it's an integer bool verifyInput = int.TryParse(Console.ReadLine(), out int userInput); //verifiy the input again if (!verifyInput || userInput <= 0) { //unexpected value Console.WriteLine("You need to type an integer number!"); } //compare the number of countries in the list with the user input and use the minimum var maxCountries = Math.Min(userInput, countries.Count) == userInput ? userInput : countries.Count; //enumerate only the countries user asked but show the option to continue displaying //enumerate all countries in the list for (int i = 0; i < countries.Count; i++) { //stop displaying when it reaches the input user if (i > 0 && (i % maxCountries == 0)) { //ask user if then want to continue Console.WriteLine("\nHit enter to contine or anything else to quit: "); //verify if the input isn't enter if (Console.ReadLine() != "") { //break the loop if it's not break; } } Country country = countries[i]; Console.WriteLine($"{i+1} : {PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)} : {country.Name}"); } //shows the number of elements in the list Console.WriteLine($"\n{countries.Count} countries"); Console.ReadLine(); }