Create list of a Data column

Hello Developers,

I want to create a list from a data column of a datatable , the column has repeated values.
so i have to fetch the distinct values from that column.

P.S - Don’t want to use invoke activity , don’t have .ToList Function

@susHI_Bae

distinctValues = (From row In dataTable.AsEnumerable()
                  Select Convert.ToString(row("ColumnName"))).Distinct().ToList()

Or

Assign activity: distinctValues = New List(Of String)()

For Each Row activity (dtData)
    If activity (Not distinctValues.Contains(row("ColumnName").ToString)) Then
        Assign activity: distinctValues.Add(row("ColumnName").ToString)
    End If
End For Each

A blunt method would be to use just native activities:
For each row in DT
if not myList.contains(row(yourcolumn)) then
add to collection list (row(yourcolumn)
end if
next

But there are probably clearer ways to do this. Not this is very much pseudo code. You’ll need to apply proper assigns, initialisations etc. as usual.

Hi @susHI_Bae

→ Use the Read range workbook activity to read the excel and store in a datatable called dt_Input.
→ Then use the below LINQ Expression to get the column names by removing the duplicates in it (Distinct) and convert it to list,

- Assign -> List_Columns = Input_dt.Columns.Cast(Of System.Data.DataColumn)().Select(Function(Column) Column.ColumnName).Distinct.tolist()

Hope it helps!!

Hi @susHI_Bae

If you dont want use the LINQ Expression then you can use the For each activity and add the Columns to list, check the below steps for better understanding,
→ Use the Read range workbook activity to read the excel and store in a datatable called dt.
→ Then use the Assign activity to initialize the List variable,

distinctValues = New List(Of String)

→ distinctValues is the List datatype variable.
→ Then use the For each activity to iterate through each column in the datatable dt.
→ Inside for each insert the If condition to remove the distinct values.

Check the below workflow for better understanding,

Hope it helps!!

any other activity I can use instead of ‘append to collection’ as I am using old version of UiPath system activity 20.10.1

Okay @susHI_Bae

Instead of using the Append Items to collection activity, you can use the assign activity and write the expression to append the data to a list,

- Assign -> distinctValues = distinctValues.Add(CurrentRow("Column Name").toString)

Hope it helps!!

got an error
“Expression does not produce a value”