Specific Columns Value Replace in the DataTable

Hi Team,

Can tell me how to change specific Columns Values in the Datatable…

i tried linq quire

Out_Dt.AsEnumerable().Where(Function(r) r(“Employee Status”).ToString.Replace(“Active”,“InActive”).ToString).CopyToDataTable

In above code getting on error String to Boolean

Thanks
shyam

@Shyam_Pragash

Please try this in invoke code add outdt as in/out argument

Out_Dt.AsEnumerable().ToList.ForEach(Sub(r) r("Employee Status") = r("Employee Status").ToString.Replace("Active","InActive").ToString)

cheers

Can tell linq quries

A where requires always a condition which evaluated to true or false

Also have a look here:
How to Update Data Column Values of a Data Table | Community Blog

Out_Dt = (From row In Out_Dt.AsEnumerable()
Let empStatus = If(row(“Employee Status”).ToString() = “Active”, “Inactive”, row(“Employee Status”).ToString())
Select row.Field(Of Object)(“Column1”), row.Field(Of Object)(“Column2”),empStatus
).CopyToDataTable()

1 Like

Hi @Mohammed_Ismail_Rahid

I have more than 85 Columns and have data… So that i want to change the specific columns value in the datatable…

Thanks
Shyam

In such cases, you can use a loop to iterate through the columns and apply the necessary changes.

1 Like

If Not linq ,Use for each row in data table and provide an if condition inside
if:CurrentRow(“Employee Status”).Equals(“Active”)
then: use Assign activity:CurrentRow(“Employee Status”)=“InActive”

Regards!

2 Likes

Out_Dt.AsEnumerable().ToList().ForEach(Sub(row)
If row(“Employee Status”).ToString() = “Active” Then
row(“Employee Status”) = “Inactive”
End If
End Sub)
Hope this helps…

1 Like

Hey Shyam, can I know the number of columns in your datatable?

The solution would be helpful if you have few numbers of datacolumns.

(
From row In dt1.Copy.AsEnumerable()
Let country = If (row(“Country”).ToString = “USA” , row(“Country”).ToString.Replace(“USA”,“US”) , row(“Country”).ToString)
Select dt1.Rows.Add(row(“City”).ToString , country)
).CopyToDataTable

My datatable had 2 columns, City and Country. In the country column, replacement is done.If you have more columns, just add those in the select line.

1 Like

Hi @Mohammad.Fuad

i have 74 Columns in the datatable.

1 Like

@Shyam_Pragash

Did you try the invoke code?it is a linq only

Cheers

oh , then mine won’t be plausible :sweat_smile: .

Hi @tazunnisa.badavide @Anil_G @Mohammed_Ismail_Rahid
@Mohammad.Fuad @ppr

Thanks all i got the solution.

2 Likes

@Shyam_Pragash

Glad itsresolved.Happy Automation

Cheers

1 Like

Can you share your solution

Cheers…

Hi @Mohammed_Ismail_Rahid

(From row In Out_Dt.AsEnumerable()
Let colInd = Out_Dt.Columns(“Employment Status”).Ordinal
Let col = If (row(“Employment Status”).ToString = “Active” , row(“Employment Status”).ToString.Replace(“Active”,“InActive”) , row(“Employment Status”).ToString)
Let ra = row.ItemArray.Take(colInd).Append(col).Concat(row.ItemArray.Skip(colInd+1)).ToArray()
Select Dt_Clone.Rows.Add(ra)).CopyToDataTable()

1 Like

Thank You…
Happy Automation

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