The Output is Expected. But it does make use of 1 For Each Row. But check if you find this useful. I’ll also check to see if it can be implemented in a Single Query and try to implement it, but mostly we’ll need to use a For Each.
If it is acceptable. Check for Different Data and provide a Feedback if have found any errors.
@GuhanTM
If a certain complexity is allowed then it can be done with the help of cartesian product and tuple within a single LINQ:
(From d In dtData.AsEnumerable
Let seg = Enumerable.Range(0,CInt(Math.Ceiling(d(1).ToString.Length)/4)).toArray
From t In seg.Select(Function (x) Tuple.Create(x, New String(d(1).ToString.Skip(x*4).Take(4).toArray))).toArray
Let ra = New Object(){d(0),t.Item2, t.Item1+1}
Select dtResult.Rows.Add(ra)).CopyToDataTable
@GuhanTM Yes. As @ppr mentioned I need to make some minor changes in the tempArray assignment. If you still want to use the Solution that I have provided, you can replace the value provided for tempArray with the below :
@GuhanTM What is the Output you get when you use it ?
I have updated my Previous Workflow to use only a Single Linq Query for the operation. Check if it meets your requirements :
@GuhanTM
it looks like a inner Bug in LINQ execution, as isolated statements are working correctly. go for this workaraound:
(From d In dtData.AsEnumerable
Let seg = Enumerable.Range(0,(d(1).ToString.Length\4)+1).toArray
From t In seg.Select(Function (x) Tuple.Create(x, New String(d(1).ToString.Skip(x*4).Take(4).toArray))).toArray
Let ra = New Object(){d(0),t.Item2, t.Item1+1}
Select dtResult.Rows.Add(ra)).CopyToDataTable
Edited:
(From d In dtData.AsEnumerable
From s In Enumerable.Range(0,CInt(Math.Ceiling(d(1).ToString.Length / 4))).toArray
Let t = New String(d(1).toString.Skip(s*4).Take(4).toArray)
Let ra = New Object(){d(0), t, s+1}
Select dtResult.Rows.Add(ra)).CopyToDataTable
@ppr if we use exactly multiples of 4-character length text ex: (Samp, SampText,…) then one more additional row got inserted in the output. Also the sequence number is starting with 0 instead of 1.
ex: Input DT
Col1 Col2
AA 1234
EE 12345678
Output DT
Col1 Col2 Col3
AA 1234 0
AA 1
BB 1234 0
BB 5678 1
BB 2
But the Required Output is: Col1 Col2 Col3
AA 1234 1