When building applications in .NET that require you to fetch country and regions etc, we mostly use an API to fetch data over the network, however, this data most of the time doesn't change. In order to improve user experience by loading data faster, you might want to reduce network calls especially in mobile platforms. This will go a long way to reduce the data consumption of your users.
Country.Data.Standard is designed to provide offline relevant country information across all .Net Platforms, below is how you can use this library in your application to achieve your goals.
Install Library
- PM> Install-Package CountryData.Standard -Version 1.0.1
Initialize the Country data object
-
- var helper = new CountryHelper()
Get list of Countries
- var countries = helper.GetCountries();
- foreach (var country in countries)
- {
- Console.WriteLine(country);
- }
Get list of Regions in a Country by Country Code
- var regions = helper.GetRegionByCountryCode("GH");
- foreach (var region in regions)
- {
- Console.WriteLine(region.Name);
- }
Using lambda for custom querries
GetCountryData() returns an IEnumerable<Country> which can be queried with Lambda for a more flexible usage.
-
- var data = helper.GetCountryData();
-
- var countries = data.Select(c => c.CountryName).ToList();
- foreach (var country in countries)
- {
- Console.WriteLine(country);
- }
-
-
-
- data.Where(x => x.CountryShortCode == "US")
- .Select(r=>r.Regions).FirstOrDefault()
- .ToList();
Adding this library to any .NET solution makes it easy for you to access offline country Data. You can contribute to this library
here. All contributions are welcomed.