How to increment numbers in a column based on values in other column? Please Help

Hi All,

Please help me to solve this
Since dealing with datatable with large data, I tried in LinQ Query instead of For Each loop:

What I have in a Datatable:
ColumnA ColumnB
A
B
A
X
C
X
A
Y

What I need:
ColumnA ColumnB
A 1
B 2
A 3
X
C 4
X
A 5
Y

LinQ Query:

What I Tried:

After cloning the Datatable “DestDT” from “SourceDT”,

Assign:
TRN = 0

Assign:
DestDT = (From a In SourceDT.AsEnumerable() Select DestDT.LoadDataRow (New Object() {
a.Field(Of Object)(“ColumnA”), If( (a.Field(Of String)(“ColumnA”)=“A” Or a.Field(Of String)(“ColumnA”)=“B” Or a.Field(Of String)(“ColumnA”)=“C”),TRN+1.ToString,"")
},False)).CopyToDataTable

What I am getting is 1 for all the matching rows. Please help

@GuhanTM
welcome to the forum

sure we will help you. Currently the requirement is not 100% derivable. Can you please share some more details with us. E.g why X is not assigned with a number.

Thanks

2 Likes

Thanks for the reply @ppr

I want to increment the value only if the ColumnA contains specific values eg: A,B or C.
For any other values, the value in the ColumnB should be empty.

@GuhanTM
find starter help here:
DT_OccurenceCounter.xaml (7.1 KB)

2 Likes

Thank you very much @ppr
It works good and result is exactly as expected.

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