How to add items into datatable with loop

datatable
activities

#1

There are
$hoge data type “system.collections.generic.icollection”
and
$fuga data type “system.data.data.table”
as follows:

$hoge
{“abc”,“def”,“ghi”}

$fuga
row(0) {“abc”,“apple”,“orrange”}
row(1) {“xyz”,“dog”,“cat”}
row(2) {“ghi”,“cherry”,“melon”}

and Im gonna match each $hoge with $fuga(0) with loop and rearrange the table as follow;
$bar data type “system.data.data.table”
row(0) {“abc”,“apple”,“orrange”}
row(1) {“ghi”,“cherry”,“melon”}

Can I make this with “if” and “foreach”?


#2

Yes, it is possible to do that using a For Each to iterate hoge and then use the Select method from fuga to check whether there are any rows containing words in hoge: UsingForEach.xaml (12.3 KB)

Another option is to use LINQ to filter fuga based on hoge (something like From fugaRow In fuga Join hogeRow In hoge On fugaRow.Field(Of String)("Col1") Equals hogeRow Select fugaRow): UsingLINQ.xaml (8.8 KB)


#3

Hi @Mateus_Cruz
It perfectly worked! Thanks for your helpful comment.
BTW, If there is data like
$piyo
row(0) {“ab c”,“apple”,“orrange”}
row(1) {“gh i”,“cherry”,“melon”}

without rearranging the tables, Can I make the items
like followings:
In the other words, Can i use destructive method for table data
like array_walk in PHP?

row(0) {“abc”,“apple”,“orrange”} // row(0)(0).relpace(" “,”")
row(1) {“ghi”,“cherry”,“melon”}// row(0)(1).relpace(" “,”")


#4

You can do something similar to array_walk by using the Select method of an Enumerable object:
myArray.Select(Function(x) x.Replace(" ", "")).ToArray()

But in the case of a DataTable, Select does something different and doesn’t accept a function.
In this case, I believe it’s better to just use a For Each activity with an Assign inside to modify the rows: ApplyFunctionToArray.xaml (9.2 KB)