Sunday, December 1, 2013

Get the list of country code and names in c#

Here is a simple Linq code to retrive the list of country code and names
C# code:

public IEnumerable<Country> GetCountries()
{
    var countryList = from r in
                      from ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                      select new RegionInfo(ci.LCID)
                      group r by r.TwoLetterISORegionName into g
                      select new Country
                      {
                          Code = g.Key,
                          Name = g.First().DisplayName
                      };
    return countryList;
}
Also you need to add the below class
class Country
{
        public string Code { get; set; }
        public string Name { get; set; }
}