Split Main DataTable and Create List of DataTable

Hi All,

I have below Main DT= DT_Main
Name, Place, Phone
Rox, US, 1234567890
Elon, UK, 2345678901
Name, Place, Phone
Raj, India, 3456789012
Rohan, India, 4567890123
Name, Place, Phone
Adam, USA, 5678901234

I want output as List (of DataTable)= ListDT
Condition: Get 1st DT as Name to Next Name Data
Example: ListDT(0)
Name, Place, Phone
Rox, US, 1234567890
Elon, UK, 2345678901
Then, ListDT(1)
Name, Place, Phone
Raj, India, 3456789012
Rohan, India, 4567890123
Then, ListData(2)
Name, Place, Phone
Adam, USA, 5678901234

One More thing, Column Name is not fixed. Its dynamic.
It may be FullName, Name, OwnerName so its not fixed.

It’ll be helpful, If I can achieve this by using linq.

@rlgandu @Nawazish_Ahmad @Anil_G @ppr @mkankatala @Shiva_Nikhil @Parvathy
Thanks in Advance

Hi,

How about the following?

First add header of the first block using InsertAt method via InvokeMethod activity.

Next

arrIdxStart = dt.AsEnumerable.Where(Function(r) r("Name").ToString="Name" AndAlso r("Place").ToString ="Place" AndAlso r("Phone").ToString="Phone").Select(Function(r) dt.Rows.IndexOf(r)).ToArray

arrIdxEnd = arrIdxStart.Skip(1).Concat({dt.Rows.Count}).ToArray

Then

listDt = arrIdxStart.Zip(arrIdxEnd,Function(s,e) dt.AsEnumerable.Skip(s+1).Take(e-s-1).CopyToDataTable).ToList

Sample
Sample20241107-1.zip (3.2 KB)

Regards,

1 Like

Hi @Yoichi

Thanks for the help but as I mentioned that. Column Name is not fixed it can be any value
Name, Place-1, Phone-1, Age-1
Rox, US, 1234567890, 49
Elon, UK, 2345678901- 35
Then, ListDT(1)
Contact, Place-2, Phone-2, Age-2
Raj, India, 3456789012, 33
Rohan, India, 4567890123, 25
Then, ListData(2)
Customer, Place-3, Phone-3, Age-3
Adam, USA, 5678901234, 21

And also I have approx 14 column.
so the first column is not fixed. and other column name is fixed but after - interger value is not fixed also.

Can you please modify according to this.

I’m trying to Split the into the list form reference.
where row contain Place- and Phone- and Age-
then take form this to next occurance of Place- and Phone- and Age-

@SP_Bot

I hope this is what you need

listdtVar = dt.AsEnumerable.Chunk(2).Select(function(x) x.copyToDataTable).ToList

Hi @Anil_G

Thanks for help.

But no the No. of rows is not fixed.

@SP_Bot

Just now saw what you need exactly…in @Yoichi code replace this

arrIdxStart = dt.AsEnumerable.Where(Function(r) r.itemArray.Any(function(x) x.ToString.ToLower.Contains("place"))).Select(Function(r) dt.Rows.IndexOf(r)).ToArray

you can replace place with any part of the column you know which is static

cheers

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