Add Serial number column using Linq Query

Hi All,

I have an excel file which has more than 6000 line items so I want to add a column in the datatable with serial numbers starting from 1 using a Linq query, as using for each row will take a lot of time.

Please can you help me on this. Any help is really appreciated.

Thank you

Hi @ashwini.bagewadi ,

Please check out this thread. This will work for your query.


we would assume that it will not take much more longer time

However the LINQ approach could look like this:



(From i In Enumerable.Range(0, dtData.Rows.Count)
Let ri = New Object(){i + 1}
Let ra = ri.Concat(dtData.Rows(i).ItemArray).ToArray
Select dtResult.Rows.Add(ra)).CopyToDataTable

or simplified to:

(From i In Enumerable.Range(0, dtData.Rows.Count)
Let ra = dtData.Rows(i).ItemArray.Prepend(i + 1).ToArray
Select dtResult.Rows.Add(ra)).CopyToDataTable

Find starter help here:
AddRowIndexToExistingDataTable.xaml (7.8 KB)

Thank you soo much @athira.somasekharan for your response, I had checked this before but I needed a solution without using Invoke code activity.

Thank you soo much @ppr for your response, you code worked perfectly fine and is giving me expected results. Thank you once again for helping me out.

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