LinQ to split a single row into multiple rows based on a cell value separated by ","

Hi,

I have a datatable similar to the one below. I need to perform an operation without using “For each” activity to attain the output mentioned below.

Input :
Column 1 | Column 2 | Column 3 |
A | B | 1,2,3 |
C | D | 4 |
E | F | 5,6 |

So, I need the output as :
Column 1 | Column 2 | Column 3 |
A | B | 1 |
A | B | 2 |
A | B | 3 |
C | D | 4 |
E | F | 5 |
E | F | 6 |

Please let know the linq query to achieve this. Thanking y’all in advance.

@prajwal_manvi1
Welcome to the forum

give a try on

(From d in dtData.AsEnumerable
From s in d(2).toString.Trim.Split(","c)
Let ra = new Object(){d(0), d(1), s}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable

1 Like

Why are you trying to avoid For Each?

1 Like

Hi,

Im sorry, I did not mention this earlier. A couple of lines also are line.

Input :
Column 1 | Column 2 | Column 3 |
A | B | 1,2,3 |
C | D | 4 |
E | F | 5,6 |

So, I need the output as :
Column 1 | Column 2 | Column 3 |
A | B | 1 |
A | B | 2 |
A | B | 3 |
C | D | 4 |
E | F | 5 |
E | F | 6 |

Thank you in advance.

this will not harm

The program gets time consuming.

we would recommend not to see LINQ usage only on the aspect of performance

1 Like

It is a myth that For Each is slower.