Help with LINQ Condition is working but there is some issue

Hi ,

I have this Linq query that helps me to country name with country code ie United states to US , United kingdom to UK

so this is what i have

Let countryName as string = row.field(of string)(“Country”).tolower.trim
Let countrycode= If(countryName = “united states”,“US”,countryName)

1sst line let me convert the country name that is there in dt into lower case
then i check if countryName matches then that countrycode is used and in new dt that will be added

Now the issue when trying same with more than 60 countries few 5-7 country codes are not getting changed can you please with any other sol. or way to coustmize

I didn’t understand exactly your issue but maybe there is some missing information that’s why some country codes are not been updated. If you want to share you project I can try to help you out!

I would suggest you to use a different optimized approach using a dictionary that stores country names and their respective codes. By using a dictionary, you can map all country names to their corresponding codes in a clean, readable, and scalable way. This will avoid any hardcoded conditions like If(countryName = "united states", "US", countryName)

Suggested Steps:

Step 1: Initialize the Dictionary

You’ll need to create the dictionary to map country names to their respective country codes.

  1. Assign Activity: Use an “Assign” activity to initialize the dictionary of country names and their codes.
  • In the To field, create a variable countryCodes of type Dictionary(Of String, String).
  • In the Value field, assign the dictionary as shown below:
countryCodes = New Dictionary(Of String, String) From {
    {"united states", "US"},
    {"united kingdom", "UK"},
    {"canada", "CA"},
    {"germany", "DE"},
    {"france", "FR"},
    {"italy", "IT"},
    {"switzerland", "CH"}
    ' Add more country names and their respective codes
}

Step 2: Loop Through DataTable Rows

Next, loop through your existing DataTable to process each row and map the country names to the country codes.

  1. For Each Row Activity: Drag a “For Each Row” activity to loop through your DataTable (dt).
  • Input: Your existing DataTable (e.g., dt).
  • Output: Each row (e.g., row).
  1. Within the For Each Row:
  • Add an “Assign” activity to extract and format the country name:
countryName = row.Field(Of String)("Country").ToLower().Trim()
* **countryName** is a `String` variable.
  • Add another “Assign” activity to find the corresponding country code from the dictionary:
countryCode = If(countryCodes.ContainsKey(countryName), countryCodes(countryName), countryName)
* **countryCode** is a `String` variable.
* If the country exists in the dictionary, you get the corresponding code; otherwise, you leave it unchanged (or apply any default value).
  1. Update the DataTable: Use an “Assign” activity or “Set Field” activity to update the row in the DataTable with the countryCode value:
row("Country") = countryCode

Step 3: Add Data to New DataTable

If you want to create a new DataTable with the updated country codes:

  1. Create a New DataTable: Use the “Build Data Table” activity to create a new DataTable with the same structure as your original one.
  2. Inside the For Each Row Loop:
  • After mapping the country name to the code, use an “Add Data Row” activity to add the updated row to the new DataTable:
    • ArrayRow: Add the entire row to the new DataTable like this:
{row("Column1"), row("Country"), row("OtherColumn")}
* Or just the relevant fields like `row("Country")` if you are updating a specific column.

Step 4: Output or Export DataTable

Once the loop is finished and you’ve updated the DataTable:

  1. Write Range Activity (if you need to write to Excel):
  • Use a “Write Range” activity to write the new DataTable back to an Excel file.
  • Specify the target sheet and range where you want to export the updated data.

Complete UiPath Flow:

  1. Assign countryCodes dictionary.
  2. Use “For Each Row” to loop through the DataTable.
  3. For each row:
  • Get the country name in lowercase and trim it.
  • Check against the dictionary and get the corresponding country code.
  • Update the row with the country code.
  1. Add the updated row to a new DataTable.
  2. Output the result by writing it back to an Excel file or use the updated DataTable as needed.

Example in UiPath:

Here’s a visual flow of the steps:

  • Assign Activity: countryCodes (Dictionary initialization)
  • For Each Row Activity: Loop through the DataTable dt
    • Assign Activity: countryName = row.Field(Of String)("Country").ToLower().Trim()
    • Assign Activity: countryCode = If(countryCodes.ContainsKey(countryName), countryCodes(countryName), countryName)
    • Assign Activity: row("Country") = countryCode
    • Add Data Row: Add the updated row to a new DataTable.
  • Write Range Activity: Export the new DataTable to Excel (if needed).
1 Like

@mint

  1. Better to create table mapping with country name and code and then use lookup datatable
  2. Alternately can use lookup datatable as well

cheers

If you have to change country codes, there are bigger problems than LINQ :slightly_smiling_face:

If you need users to maintain this data, I would add the table to UiPath Data Service or at a minimum add the data to an Excel or CSV file.
You just read the data from Excel/CSV to a data table and do filtering or look up.
With Data Service you can run query against the database: Data Service - Introduction

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