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:

  1. public IEnumerable<Country> GetCountries()
  2. {
  3.     var countryList = from r in
  4.                       from ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
  5.                       select new RegionInfo(ci.LCID)
  6.                       group r by r.TwoLetterISORegionName into g
  7.                       select new Country
  8.                       {
  9.                           Code = g.Key,
  10.                           Name = g.First().DisplayName
  11.                       };
  12.     return countryList;
  13. }
Also you need to add the below class
  1. class Country
  2. {
  3. public string Code { get; set; }
  4. public string Name { get; set; }
  5. }

No comments:

Post a Comment