Data table - split data into batches/set based on grouping some records together

Hi All,

I have the requirement as below, any help to address this in easier way is much appreciated
need to split the Material as batches

Eg1
If Split Material per batch as 5 then as the total no.of record count in the below table is 10 we should update 1st 5 materials as 1 the subsequent 5 materials as 2
Batch Material
1 M1
1 M2
1 M3
1 M4
1 M5
2 M6
2 M7
2 M8
2 M9
2 M10
Eg2
If Split Material per batch as 3 then as the total no.of record count in the below table is 10 we should update 1st 3 materials as 1 the subsequent 3 materials as 2 and so on
Batch Material
1 M1
1 M2
1 M3
2 M4
2 M5
2 M6
3 M7
3 M8
3 M9
4 M10

@syed.khaja

You can use the below

Dt.AsEnumerable.chunks(GroupCount) in a for loop list items

And inside loop currentitem.CopyToDatatable will give eqch set as you need in the loop till all items are completed

Groupcount is the number of items you need like 5,3 etc

Cheers

Hi @syed.khaja

Can you try the below

DT.AsEnumerable.Chunk(int_batchCount).Select(Function (x) x.CopyToDataTable).ToList

Sequence26.xaml (10.2 KB)

Output:

Records.xlsx (9.4 KB)

Regards,

Thanks @Anil_G and @lrtetala for your help. I am getting the chunk as required.

However one small query , the original DT is as below (Batch column values not updated) , we have to update that based on the Group count value which comes dynamically.
DT Initial State:
Batch Material
M1
M2
M3
M4
M5
M6
M7
M8
M9
M10
DT after gGropu Count is provided: eg if Group Count is provided as 5 then the 1st set of 5 materials should have value 1 and the next set should be incremented and should have value as 2
Batch Material
1 M1
1 M2
1 M3
1 M4
1 M5
2 M6
2 M7
2 M8
2 M9
2 M10
I am assigning like below
currentItem.Columns(Batch).Expression = counter.ToString (counter is maintained to assign the batch number)
is there any way to assign the values directly to Main DT in the loop or I have to build a DT and append the currentItem records to it each time ?

@syed.khaja

Actually merge datatable inside a loop will do the job…you can use for loop counter to add different sequence numbers

Cheers

@syed.khaja

Can you try like below

Sequence26.xaml (18.6 KB)

Output:

Records.xlsx (9.5 KB)

Regards,

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