How to create Datatable from another Datatable copying partial rows and columns

Hello,

I have a scenario where each entity data need to be retrieved from a single datatable by converting into multiple datatables.

Ex:
image

to three seperate datatables like below, How to achieve this?

image

Thanks,
GK

Hi,

If number of rows of divided datatable is always 4, the following will work.

arrDt = Enumerable.Range(0, CInt(Math.Floor(dt.Rows.Count / 4))).Select(Function(i) dt.AsEnumerable.Skip(4*i).Take(4).CopyToDataTable()).ToArray

Sample20220403-2.zip (3.0 KB)

Note: this sample write each data to Sheet0, Sheet1 etc.

Regards,

1 Like

Thank You Yoichi,

yeah we have the scenario where the number of entries changes and not fixed for each entity,

I’ve got struck here how to achieve this, Please provide ideas for this scenario also.

Thanks,
Kalyan

Hi,

For example, first cell of divided datatable always starts “Name”, the following will work. Can you try this?

arrPos = dt.AsEnumerable.Where(Function(r,i) r(0).ToString.StartsWith("Name")).Select(Function(r) dt.Rows.IndexOf(r)).ToArray()
arrLength = arrPos.Zip(arrPos.Skip(1).Concat({dt.Rows.Count}),Function(i1,i2) i2-i1).ToArray
arrDt = arrPos.Select(Function(v,i) dt.AsEnumerable.Skip(v).Take(arrLength(i)).CopyToDataTable).ToArray

Sample20220403-2v2.zip (3.1 KB)

Regards,

1 Like

Thank You,

I will try this

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