How to convert IEnumerable to Datatable?

I’m reading excel row using ''Read Row" which is storing value as variable type ‘IEnumerable’.

I want to convert this ‘IEnumerable’ to variable type ‘DataTable’

How to achieve this ?

Hi @Muralikrishna_Surve ,

Could you try declaring a Variable of type DataTable and assign that to the Read Range Activity?
Usually when you create a variable using Ctrl+K in the Read Range Output it generates a Datatable.

As for converting an IEnumerable to DataTable, if its an IEnumerable of DataRows, then you have to append a .CopyToDataTable() method to it.

Kind Regards,
Ashwin A.K

1 Like

Can you give example for how to append a .CopyToDataTable() method to it ?

Hi @Muralikrishna_Surve ,

I apologize, I misread your question.
Since you are using a Read Row Activity, you have to declare a DataTable in advance so that you can add the row you just read to it:

image

We usually read the entire range and store it inside of a Data Table.
Is there any specific reason as to why you are reading it row by row?

Kind Regards,
Ashwin A.K

Thank you.

But is there any other way to convert this ? because here we need to declare a DataTable in advance but we don’t have exact number of rows available in ReadRow.

Hi @Muralikrishna_Surve ,

That can be easily taken care of.
If needed, we can even add the actual headers, but if the headers are not important then you can try this.
First we read the Row, then initialize the DataTable before we use a For Each Activity to keep adding DataColumns to fit the schema:

image

Then we add the row to our newly created DataTable.

image

ReadRowConvertToDT.xaml (6.7 KB)

Kind Regards,
Ashwin A.K

1 Like

Thanks buddy

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