Add primary key to an existing datatable

Hi All,

I need to add a new column to an existing datatable and populate it with unique values. Could you provide guidance on how to achieve this without using iteration?

Why am I doing this?
Occasionally, my datatable contains duplicate rows, making it difficult to distinguish between them. Adding a column with unique values will help differentiate between these duplicate rows.

Thanks,
Kapil

Hey @Kapil

Check this XAML out. No iterations/loops needed.
Datatable_AddColumn_RowIndex.xaml (11.5 KB)

Method Steps:
Step 1 Build a datatable (called DT1) and remove all columns and rows.
Step 2: Add a new column with ‘Auto Increment’ enabled and datatype ‘Int32’.
This image shows the "Edit Column" settings dialog in a database management interface, with the column name "IDX", data type "Int32", and the "Auto Increment" option selected. (Captioned by AI)

Step 3: Insert a Merge DataTable activity. With the following settings.
Destination: DT1
MissingSchemaAction: Add
Source: Datatable_Original

Hopefully this helps.

Let us know how you go.

Cheers

Steve

Hi @Kapil

Use below query and then use Merge DataTable activity

DT.AsEnumerable() _
           .GroupBy(Function(row) row("ColumnName").ToString) _
           .Select(Function(group) group.First()) _
           .CopyToDataTable()

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