Here is a class I wrote to convert different styles to ISO3166 and can also be used for this case. If looping over many records, I would use a dictionary that is initialized once or this can be adapted to do so.
using System;
namespace Core.ISO
{
internal class UsStateCode_Iso3166
{
private UsStateCode_Iso3166(string name,
string isoCode,
string alpha2)
{
Name = name;
IsoCode = isoCode;
Alpha2 = alpha2;
}
public string Name { get; }
public string IsoCode { get; }
public string Alpha2 { get; }
public static UsStateCode_Iso3166 Alabama { get { return new UsStateCode_Iso3166("Alabama", "US-AL", "AL"); } }
public static UsStateCode_Iso3166 Alaska { get { return new UsStateCode_Iso3166("Alaska", "US-AK", "AK"); } }
public static UsStateCode_Iso3166 AmericanSamoa { get { return new UsStateCode_Iso3166("American Samoa", "US-AS", "AS"); } }
public static UsStateCode_Iso3166 Arizona { get { return new UsStateCode_Iso3166("Arizona", "US-AZ", "AZ"); } }
public static UsStateCode_Iso3166 Arkansas { get { return new UsStateCode_Iso3166("Arkansas", "US-AR", "AR"); } }
public static UsStateCode_Iso3166 California { get { return new UsStateCode_Iso3166("California", "US-CA", "CA"); } }
public static UsStateCode_Iso3166 Colorado { get { return new UsStateCode_Iso3166("Colorado", "US-CO", "CO"); } }
public static UsStateCode_Iso3166 Connecticut { get { return new UsStateCode_Iso3166("Connecticut", "US-CT", "CT"); } }
public static UsStateCode_Iso3166 Delaware { get { return new UsStateCode_Iso3166("Delaware", "US-DE", "DE"); } }
public static UsStateCode_Iso3166 DistrictOfColumbia { get { return new UsStateCode_Iso3166("District Of Columbia", "US-DC", "DC"); } }
public static UsStateCode_Iso3166 Florida { get { return new UsStateCode_Iso3166("Florida", "US-FL", "FL"); } }
public static UsStateCode_Iso3166 Georgia { get { return new UsStateCode_Iso3166("Georgia", "US-GA", "GA"); } }
public static UsStateCode_Iso3166 Guam { get { return new UsStateCode_Iso3166("Guam", "US-GU", "GU"); } }
public static UsStateCode_Iso3166 Hawaii { get { return new UsStateCode_Iso3166("Hawaii", "US-HI", "HI"); } }
public static UsStateCode_Iso3166 Idaho { get { return new UsStateCode_Iso3166("Idaho", "US-ID", "ID"); } }
public static UsStateCode_Iso3166 Illinois { get { return new UsStateCode_Iso3166("Illinois", "US-IL", "IL"); } }
public static UsStateCode_Iso3166 Indiana { get { return new UsStateCode_Iso3166("Indiana", "US-IN", "IN"); } }
public static UsStateCode_Iso3166 Iowa { get { return new UsStateCode_Iso3166("Iowa", "US-IA", "IA"); } }
public static UsStateCode_Iso3166 Kansas { get { return new UsStateCode_Iso3166("Kansas", "US-KS", "KS"); } }
public static UsStateCode_Iso3166 Kentucky { get { return new UsStateCode_Iso3166("Kentucky", "US-KY", "KY"); } }
public static UsStateCode_Iso3166 Louisiana { get { return new UsStateCode_Iso3166("Louisiana", "US-LA", "LA"); } }
public static UsStateCode_Iso3166 Maine { get { return new UsStateCode_Iso3166("Maine", "US-ME", "ME"); } }
public static UsStateCode_Iso3166 Maryland { get { return new UsStateCode_Iso3166("Maryland", "US-MD", "MD"); } }
public static UsStateCode_Iso3166 Massachusetts { get { return new UsStateCode_Iso3166("Massachusetts", "US-MA", "MA"); } }
public static UsStateCode_Iso3166 Michigan { get { return new UsStateCode_Iso3166("Michigan", "US-MI", "MI"); } }
public static UsStateCode_Iso3166 Minnesota { get { return new UsStateCode_Iso3166("Minnesota", "US-MN", "MN"); } }
public static UsStateCode_Iso3166 Mississippi { get { return new UsStateCode_Iso3166("Mississippi", "US-MS", "MS"); } }
public static UsStateCode_Iso3166 Missouri { get { return new UsStateCode_Iso3166("Missouri", "US-MO", "MO"); } }
public static UsStateCode_Iso3166 Montana { get { return new UsStateCode_Iso3166("Montana", "US-MT", "MT"); } }
public static UsStateCode_Iso3166 Nebraska { get { return new UsStateCode_Iso3166("Nebraska", "US-NE", "NE"); } }
public static UsStateCode_Iso3166 Nevada { get { return new UsStateCode_Iso3166("Nevada", "US-NV", "NV"); } }
public static UsStateCode_Iso3166 NewHampshire { get { return new UsStateCode_Iso3166("New Hampshire", "US-NH", "NH"); } }
public static UsStateCode_Iso3166 NewJersey { get { return new UsStateCode_Iso3166("New Jersey", "US-NJ", "NJ"); } }
public static UsStateCode_Iso3166 NewMexico { get { return new UsStateCode_Iso3166("New Mexico", "US-NM", "NM"); } }
public static UsStateCode_Iso3166 NewYork { get { return new UsStateCode_Iso3166("New York", "US-NY", "NY"); } }
public static UsStateCode_Iso3166 NorthCarolina { get { return new UsStateCode_Iso3166("North Carolina", "US-NC", "NC"); } }
public static UsStateCode_Iso3166 NorthDakota { get { return new UsStateCode_Iso3166("North Dakota", "US-ND", "ND"); } }
public static UsStateCode_Iso3166 NorthernMarianaIslands { get { return new UsStateCode_Iso3166("Northern Mariana Islands", "US-MP", "MP"); } }
public static UsStateCode_Iso3166 Ohio { get { return new UsStateCode_Iso3166("Ohio", "US-OH", "OH"); } }
public static UsStateCode_Iso3166 Oklahoma { get { return new UsStateCode_Iso3166("Oklahoma", "US-OK", "OK"); } }
public static UsStateCode_Iso3166 Oregon { get { return new UsStateCode_Iso3166("Oregon", "US-OR", "OR"); } }
public static UsStateCode_Iso3166 Pennsylvania { get { return new UsStateCode_Iso3166("Pennsylvania", "US-PA", "PA"); } }
public static UsStateCode_Iso3166 PuertoRico { get { return new UsStateCode_Iso3166("Puerto Rico", "US-PR", "PR"); } }
public static UsStateCode_Iso3166 RhodeIsland { get { return new UsStateCode_Iso3166("Rhode Island", "US-RI", "RI"); } }
public static UsStateCode_Iso3166 SouthCarolina { get { return new UsStateCode_Iso3166("South Carolina", "US-SC", "SC"); } }
public static UsStateCode_Iso3166 SouthDakota { get { return new UsStateCode_Iso3166("South Dakota", "US-SD", "SD"); } }
public static UsStateCode_Iso3166 Tennessee { get { return new UsStateCode_Iso3166("Tennessee", "US-TN", "TN"); } }
public static UsStateCode_Iso3166 Texas { get { return new UsStateCode_Iso3166("Texas", "US-TX", "TX"); } }
public static UsStateCode_Iso3166 UnitedStatesMinorOutlyingIslands { get { return new UsStateCode_Iso3166("United States Minor Outlying Islands", "US-UM", "UM"); } }
public static UsStateCode_Iso3166 Utah { get { return new UsStateCode_Iso3166("Utah", "US-UT", "UT"); } }
public static UsStateCode_Iso3166 Vermont { get { return new UsStateCode_Iso3166("Vermont", "US-VT", "VT"); } }
public static UsStateCode_Iso3166 VirginIslands { get { return new UsStateCode_Iso3166("Virgin Islands", "US-VI", "VI"); } }
public static UsStateCode_Iso3166 Virginia { get { return new UsStateCode_Iso3166("Virginia", "US-VA", "VA"); } }
public static UsStateCode_Iso3166 Washington { get { return new UsStateCode_Iso3166("Washington", "US-WA", "WA"); } }
public static UsStateCode_Iso3166 WestVirginia { get { return new UsStateCode_Iso3166("West Virginia", "US-WV", "WV"); } }
public static UsStateCode_Iso3166 Wisconsin { get { return new UsStateCode_Iso3166("Wisconsin", "US-WI", "WI"); } }
public static UsStateCode_Iso3166 Wyoming { get { return new UsStateCode_Iso3166("Wyoming", "US-WY", "WY"); } }
public static UsStateCode_Iso3166 None { get { return new UsStateCode_Iso3166("N/A", "", ""); } }
public static UsStateCode_Iso3166 FromString(string str)
{
return str switch
{
"" => None,
"ALABAMA" => Alabama,
"ALASKA" => Alaska,
"AMERICAN SAMOA" => AmericanSamoa,
"ARIZONA" => Arizona,
"ARKANSAS" => Arkansas,
"CALIFORNIA" => California,
"COLORADO" => Colorado,
"CONNECTICUT" => Connecticut,
"DELAWARE" => Delaware,
"DISTRICT OF COLUMBIA" => DistrictOfColumbia,
"FLORIDA" => Florida,
"GEORGIA" => Georgia,
"GUAM" => Guam,
"HAWAII" => Hawaii,
"IDAHO" => Idaho,
"ILLINOIS" => Illinois,
"INDIANA" => Indiana,
"IOWA" => Iowa,
"KANSAS" => Kansas,
"KENTUCKY" => Kentucky,
"LOUISIANA" => Louisiana,
"MAINE" => Maine,
"MARYLAND" => Maryland,
"MASSACHUSETTS" => Massachusetts,
"MICHIGAN" => Michigan,
"MINNESOTA" => Minnesota,
"MISSISSIPPI" => Mississippi,
"MISSOURI" => Missouri,
"MONTANA" => Montana,
"NEBRASKA" => Nebraska,
"NEVADA" => Nevada,
"NEW HAMPSHIRE" => NewHampshire,
"NEW JERSEY" => NewJersey,
"NEW MEXICO" => NewMexico,
"NEW YORK" => NewYork,
"NORTH CAROLINA" => NorthCarolina,
"NORTH DAKOTA" => NorthDakota,
"NORTHERN MARIANA ISLANDS" => NorthernMarianaIslands,
"OHIO" => Ohio,
"OKLAHOMA" => Oklahoma,
"OREGON" => Oregon,
"PENNSYLVANIA" => Pennsylvania,
"PUERTO RICO" => PuertoRico,
"RHODE ISLAND" => RhodeIsland,
"SOUTH CAROLINA" => SouthCarolina,
"SOUTH DAKOTA" => SouthDakota,
"TENNESSEE" => Tennessee,
"TEXAS" => Texas,
"UNITED STATES MINOR OUTLYING ISLANDS" => UnitedStatesMinorOutlyingIslands,
"UTAH" => Utah,
"VERMONT" => Vermont,
"VIRGIN ISLANDS" => VirginIslands,
"VIRGINIA" => Virginia,
"WASHINGTON" => Washington,
"WEST VIRGINIA" => WestVirginia,
"WISCONSIN" => Wisconsin,
"WYOMING" => Wyoming,
"AL" => Alabama,
"AK" => Alaska,
"AS" => AmericanSamoa,
"AZ" => Arizona,
"AR" => Arkansas,
"CA" => California,
"CO" => Colorado,
"CT" => Connecticut,
"DE" => Delaware,
"DC" => DistrictOfColumbia,
"FL" => Florida,
"GA" => Georgia,
"GU" => Guam,
"HI" => Hawaii,
"ID" => Idaho,
"IL" => Illinois,
"IN" => Indiana,
"IA" => Iowa,
"KS" => Kansas,
"KY" => Kentucky,
"LA" => Louisiana,
"ME" => Maine,
"MD" => Maryland,
"MA" => Massachusetts,
"MI" => Michigan,
"MN" => Minnesota,
"MS" => Mississippi,
"MO" => Missouri,
"MT" => Montana,
"NE" => Nebraska,
"NV" => Nevada,
"NH" => NewHampshire,
"NJ" => NewJersey,
"NM" => NewMexico,
"NY" => NewYork,
"NC" => NorthCarolina,
"ND" => NorthDakota,
"MP" => NorthernMarianaIslands,
"OH" => Ohio,
"OK" => Oklahoma,
"OR" => Oregon,
"PA" => Pennsylvania,
"PR" => PuertoRico,
"RI" => RhodeIsland,
"SC" => SouthCarolina,
"SD" => SouthDakota,
"TN" => Tennessee,
"TX" => Texas,
"UM" => UnitedStatesMinorOutlyingIslands,
"UT" => Utah,
"VT" => Vermont,
"VI" => VirginIslands,
"VA" => Virginia,
"WA" => Washington,
"WV" => WestVirginia,
"WI" => Wisconsin,
"WY" => Wyoming,
"US-AL" => Alabama,
"US-AK" => Alaska,
"US-AS" => AmericanSamoa,
"US-AZ" => Arizona,
"US-AR" => Arkansas,
"US-CA" => California,
"US-CO" => Colorado,
"US-CT" => Connecticut,
"US-DE" => Delaware,
"US-DC" => DistrictOfColumbia,
"US-FL" => Florida,
"US-GA" => Georgia,
"US-GU" => Guam,
"US-HI" => Hawaii,
"US-ID" => Idaho,
"US-IL" => Illinois,
"US-IN" => Indiana,
"US-IA" => Iowa,
"US-KS" => Kansas,
"US-KY" => Kentucky,
"US-LA" => Louisiana,
"US-ME" => Maine,
"US-MD" => Maryland,
"US-MA" => Massachusetts,
"US-MI" => Michigan,
"US-MN" => Minnesota,
"US-MS" => Mississippi,
"US-MO" => Missouri,
"US-MT" => Montana,
"US-NE" => Nebraska,
"US-NV" => Nevada,
"US-NH" => NewHampshire,
"US-NJ" => NewJersey,
"US-NM" => NewMexico,
"US-NY" => NewYork,
"US-NC" => NorthCarolina,
"US-ND" => NorthDakota,
"US-MP" => NorthernMarianaIslands,
"US-OH" => Ohio,
"US-OK" => Oklahoma,
"US-OR" => Oregon,
"US-PA" => Pennsylvania,
"US-PR" => PuertoRico,
"US-RI" => RhodeIsland,
"US-SC" => SouthCarolina,
"US-SD" => SouthDakota,
"US-TN" => Tennessee,
"US-TX" => Texas,
"US-UM" => UnitedStatesMinorOutlyingIslands,
"US-UT" => Utah,
"US-VT" => Vermont,
"US-VI" => VirginIslands,
"US-VA" => Virginia,
"US-WA" => Washington,
"US-WV" => WestVirginia,
"US-WI" => Wisconsin,
"US-WY" => Wyoming,
_ => throw new ArgumentOutOfRangeException(nameof(str)),
};
}
}
}
It can be called like below...
void test()
{
UsStateCode_Iso3166 stateByName = UsStateCode_Iso3166.FromString("New York");
UsStateCode_Iso3166 stateByAbbrev = UsStateCode_Iso3166.FromString("NY");
UsStateCode_Iso3166 stateByIso = UsStateCode_Iso3166.FromString("US-NY");
Console.WriteLine($"Name: {stateByAbbrev.Name}, Abbrev: {stateByAbbrev.Alpha2}, ISO3166: {stateByAbbrev.IsoCode}");
//output: Name: New York, Abbrev: NY, ISO3166: US-NY
}