Initialize DataColumn

Hi,

I need to initialize a datacolumn with an ienumerable without a loop, is it possible? Something like this in an assign activity: new DataColumn(“myColumn”, myIEnumerable)

Thanks in advance,
María

Hi @mmarcos

You cannot directly initialize a DataColumn with an IEnumerable without using a loop or some form of iteration. The DataColumn constructor does not accept an IEnumerable as a parameter.

However, you can achieve this by using LINQ to convert the IEnumerable to a DataTable and then extract the desired DataColumn:

Dim myIEnumerable As IEnumerable(Of Object) = { "Value1", "Value2", "Value3" }

Dim dataTable As DataTable = myIEnumerable.Select(Function(x) New With {.myColumn = x}).CopyToDataTable()
Dim myColumn As DataColumn = dataTable.Columns("myColumn")

Hope this helps,
Best Regards.

The datatable where I need to put the column is already inizializate. Like this:
Name Age City
Maria 22
Paula 25
Cris 23
Celia 35

And the values of ‘City’ are in an IEnumerable: [Madrid, Valencia, Barcelona, Roma]. How can I add this to the ‘City’ column?

your initial requirement differs from the later mentioned case

We do understand:
IEnumerable: [Madrid, Valencia, Barcelona, Roma]
is to set value wise on a dt
Name Age City
Maria 22
Paula 25
Cris 23
Celia 35

with result
Name Age City
Maria 22 Madrid
Paula 25 Valencia
Cris 23 Barcelona
Celia 35 Roma

One of many options

For each row in datatable activity | currentrow in YourDataTableVar - set output index: idx
grafik

  • assign activity: currentrow(City) = yourIEnumerableVar(idx)

I need to do it without a loop

Have a look here for different options
How to Update Data Column Values of a Data Table | Community Blog

then use LINQ and DataTable Reconstruction Approach

There is also the Code Invoke option, but is creating a Blackbox and so less to recommend when there is no strong reason to use it