LINQ Query - Country Code Lookup

I have a variable that I scrape a country to called “PRIORITY COUNTRY 1”

I need to lookup that variable in a DT and return the country code to the variable.

Country is in the second column and code is in the first
|Code|Country|
|00|N/A |
|AD|Andorra |
|AE|United Arab Emirates |
|AF|Afghanistan |
|AG|Antigua and Barbuda |
|AI|Anguilla |
|AL|Albania |
|AM|Armenia |
|AN|Fmr. Netherlands Antilles |
|AO|Angola |
|AP|ARIPO |
|AQ|Antarctica |
|AR|Argentina |
|AS|American Samoa |
|AT|Austria |
|AU|Australia |
|AW|Aruba |
etc

lets assume: codes and countries are within a datatable - dtData

Then we can create and use a lookup dictionary

Assign Activity:
dictCCode = Dictionary(Of String, String) =

dtData.AsEnumrable().ToDictionary(Function (x) x(0).toString.Trim,Function (x) x(1).toString.Trim

and can get the Country by dictCCode(“YourCountryCodeOrKeyVar”)

1 Like

Hi @rmorgan

Try this

countryCode = (From row In dtCountries.AsEnumerable()
               Where row("Country").ToString.Equals(countryName, StringComparison.OrdinalIgnoreCase)
               Select row("Code").ToString).FirstOrDefault()

Regards,

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.