Get string array from datatable column

Hey,
I have a datatable with string entries and I want to retrieve a string array from a certain column. I tried

yourdatatablename.AsEnumerable().Select(Function (a) a.Field(of string)(“yourcolumnname”).ToString).ToArray()

but it throws an error saying “‘Field’ is not a member of ‘Char’”. Can anyone help?

Best,
Fredi

@FJJJ

Welcome to our UiPath community.

Try below expression.

         yourdatatablename.AsEnumerable().Select(Function (a) a.Field(of string)(“yourcolumnname”)).ToArray()

Hi,

Probably, type of your yourdatatablename is String. Can you try to modify its type to DataTable?

Regards,

Hi
Your expression is absolutely right

That’s how we get the column value as a array

But I feel the error is with the variable Yourdatatablename
Check whether it is a datatable or some other variable type
Change the type of that variable and give a try

Cheers @FJJJ

Hi
and thank you all for the quick responses! It was indeed a wrong datatype for the datatable, my bad :sweat:
However this is just one step of what I want to get: I want to extract something like a dictionary that consists of keys each one representing the occurence of a certain element in the column and the corresponding value being an int32 array, that contains all the indices of the rows, in which the key value appears in the column.
Example for column4:
Capture

…should result in a dictionary of:
{{“element1”, {0,1,5}},
{“element2”, {2,6,8}},
{“element3”, {3,4,7}}}

Is there a better and quicker way than iterating through all the rows in a for loop for getting the int32 arrays?

Best,
Fredi

Why do you want to avoid a for each loop? Do you have a big excel file?

yes, it has about 20000 rows and I already tried and it took about 30min

Okay, I’m working on it.

Here is the workflow:

Forum_LINQElementsIndex.xaml (7.2 KB)

Let me know if it works well with you and how much time it will take.

Best,
Charbel

Hi,

FYI, another solution using Invoke Code:

img20210930-5

dt = dt.AsEnumerable.Select(Function(r,i) 
    If (Not dict.ContainsKey(r("col4").ToString)) Then
    dict.Add(r("col4").ToString,New List(Of Int32))
    End If
        dict(r("col4").ToString).Add(i)
    Return r
End Function
).CopyToDataTable

Sequence2.xaml (10.1 KB)

Regards,

1 Like

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