private void SetCountry(CountryViewModel country, NpgsqlDataReader countryDR, List<CountryLanguage> languages) { country.Code = countryDR[0].ToString(); country.Continent = countryDR[1].ToString(); country.Country = countryDR[2].ToString(); country.Population = int.Parse(countryDR[3].ToString()); country.Languages = languages; }
public List<CountryViewModel> GetCountries(string countryQuery = "") { List<CountryViewModel> countries = new List<CountryViewModel>(); try { PsgsqlConnection.Open(); NpgsqlCommand command = new NpgsqlCommand(string.IsNullOrEmpty(countryQuery) ? Resources.getCountriesQuery : countryQuery, PsgsqlConnection); NpgsqlDataReader dr = command.ExecuteReader(); while (dr.Read()) { CountryLanguage language = new CountryLanguage(); CountryViewModel country = new CountryViewModel(); if (countries.Exists(item => item.Code.Equals(dr[0].ToString()))) { SetCountryLanguage(language, dr); countries.Where(item => item.Code.Equals(dr[0].ToString())).Single().Languages.Add(language); } else { List<CountryLanguage> LanguagesList = new List<CountryLanguage>(); if (!dr[4].GetType().Name.Equals("DBNull")) { SetCountryLanguage(language, dr); LanguagesList.Add(language); } SetCountry(country, dr, LanguagesList); countries.Add(country); } } dr.Close(); } finally { PsgsqlConnection.Close(); } return countries; }