LINQ instead of an For each and IF

dummy.xlsx (21.5 KB)
Hi Team,

I want to club the data with respect to rows, i.e. i want 100 rows in a data in 1 row, like if my input contains 800 rows then my out dt will contain 8 rows. I am attaching an excel with input in my Input Sheet and output in result Sheet.
I am also attaching my workflow here, i have done this entire thing with the help of for each and if condition and it is running perfectly file but i want it to be done with the help of LINQ. Please help team.
Main.xaml (23.3 KB)

@supermanPunch or @Yoichi Any help?

@Brian_Mathew_Maben Any help mate?

  • read range - dtData
  • Assign activity: SegmentSize | Int32 = 100
  • Build DataTable Actitvity: Configure StartIndex, EndIndex Column - dtResult
  • Assign Activity: Segments | Int32 = CInt(Math.Ceiling(dtData.Rows.Count / SegmentSize ))
  • Assign Activity: dtResult =
(From i in Enumerable.Range(0, Segments)
Let si = dtData.AsEnumerable.Skip(SegmentSize*i)("Starting Index").toString.Trim
Let ei = dtData.AsEnumerable.Skip(SegmentSize*i).Take(SegmentSize)("Ending Index").toString.Trim
Let ra = new Object(){si,ei}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable

then write back dtResult to a new Excel WorkSheet

Hi,

My result is difference with yours. Can you check it in details?

image

dtRresult = Enumerable.Range(0,dt.Rows.Count \100 - CInt(dt.Rows.Count mod 100 >0)).Select(Function(i) dtResult.LoadDataRow({dt.AsEnumerable.Skip(i*100).First().Item("Starting Index"),dt.AsEnumerable.Take(i*100+100).Last().Item("Ending Index")},False)).CopyToDataTable()

Sample20230728-5.zip (28.4 KB)

Regards,

1 Like

@Yoichi , actually my one was increasing with the number of times the loop was running and that was wrong. The solution that you gave works perfectly fine. Thanks a lot.
Can you explain the logic behind this query? I am actually confused about how you managed for that last 45 entries.

LINQ Take method returns specific number of item. if There are less items in the collection it returns all the remaining without error. So it’s unnecessary to take care last chunk carefully.

Regards,

1 Like

Understood. Thanks @Yoichi

1 Like

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