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.
ppr
(Peter Preuss)
March 25, 2022, 1:24pm
2
@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
postwick
(Paul Ostwick)
March 25, 2022, 1:46pm
3
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.
The program gets time consuming.
ppr
(Peter Preuss)
March 25, 2022, 2:59pm
7
we would recommend not to see LINQ usage only on the aspect of performance
1 Like
postwick
(Paul Ostwick)
March 25, 2022, 3:13pm
8
It is a myth that For Each is slower.
I’ve seen the discussion many times about avoiding For Each (Row) because looping through a lot of rows is inefficient.
So I wrote a test. It creates a datatable of however many rows we want (Name, Age, City). Default values are Mary, 28, Dallas. Every 10th row is Tom, 27, Miami. Goal is to update Tom to Boston.
[image]
The first method is simply a single For Each with nested Assign to change Tom’s City to Boston.
[image]
This is the method of filtering into two separate DTs so you ha…
[image]
[image]
No time difference for over 1000 rows.
[image]