Move every second row from datatable into different column

Hello Everyone,
I need to move every second row into different column in datatable.
I have datatable

Data
NameA
AddressA
NameB
AddressB
NameC
AddressC

And i need new datatable looks like this

Name	Address
NameA	AddressA
NameB	AddressB
NameC	AddressC

@Katerina_Chabova

(From row In Dt.AsEnumerable()
            Group row By index = Dt.Rows.IndexOf(row) \ 2 Into Group
            Select Dt1.Clone().Rows.Add(Group.SelectMany(Function(r) r.ItemArray).ToArray())
           ).CopyToDataTable()
1 Like

Hi @Katerina_Chabova

→ Build Data Table
image
Output: dt_Input
→ Build Data Table
image
Output: dt_Output
→ Use the below syntax in assign activity:

dt_Output= (From i In Enumerable.Range(0, dt_Input.Rows.Count \ 2)
                Let nameRow = dt_Input.Rows(i * 2)("Data").ToString()
                Let addressRow = dt_Input.Rows(i * 2 + 1)("Data").ToString()
                Select dt_Output.Clone().Rows.Add(nameRow, addressRow)
               ).CopyToDataTable()

→ Use dt_Output to print it in excel using Write Range Workbook.

Regards

1 Like

Hi @Katerina_Chabova ,

Maybe also an Alternate with Skip and Take methods like below :

OutputDT = (From i In Enumerable.Range(0,CInt(Math.Ceiling(DT.Rows.Count/2)))
Let arr = DT.AsEnumerable.Skip(CInt(i*2)).Take(2).Select(Function(y)y(0).ToString).ToArray
Select OutputDT.Rows.Add(arr)).CopyToDatatable

Here, DT is the input datatable with the First Column containing values and OutputDT is a datatable which is prepared as needed for the Output Representation (Two Columns) using Build Datatable Activity.

From Debug :

1 Like

Along with the typical preparation of dtResult

dtResult=

dtData.AsEnumerable.Select(Function (x) x(0)).Chunk(2).Select(Function (ra) dtResult.Rows.Add(ra)).CopyToDataTable

grafik

3 Likes

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