0

I want to be able to get country code using country name. As of now I'm doing this:

var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                         .Select(x => new RegionInfo(x.LCID));

var selectedRegion = regions.FirstOrDefault(region => 
                                                region.EnglishName.Contains(countryName));

Where CountryName in my case is "Tanzania". It is not giving the country code even though in the regions it is present. It always returns null.

Can you please suggest/enlighten me on how to get this done.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
coders
  • 719
  • 1
  • 11
  • 35
  • 3
    Are you sure it's present? After googling, it seems that C#'s culture list doesn't include Tanzania. – Unlocked Mar 29 '18 at 02:37

1 Answers1

5

The CultureInfo type has an EnglishName property which does contain an English (Tanzania) but as far as I can see not the region.

var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var tanzaniaCulture = cultures.FirstOrDefault(i => i.EnglishName == "English (Tanzania)");
Dan D
  • 2,493
  • 15
  • 23
  • Change a method name to GetCultures. :P – Gaurang Dave Mar 29 '18 at 03:12
  • @Dan D Thanks a lot for the info. Will this work for all the Countries? – coders Mar 29 '18 at 03:36
  • What if i have country name like this "Niederlande" which is in german i guess, the suggested code fails. How to handle such cases? – coders Mar 29 '18 at 04:29
  • @GaurangDave thanks for the feedback, I've updated the answer. I believe you'll have to install a language pack and set your culture to German in order to handle "Niederlande" – Dan D Mar 29 '18 at 14:13
  • @DanD after installing the language pack of respective one, will "English (Country)" will work should i need to replace "English" with its respective language name? – coders Apr 01 '18 at 14:58