How to replace string in data table using LINQ queries?

Hi All,

I want to replace “Failed” value with “error” in status row using LINQ query …below is my Data table …can someone please help ?

Thanks,
Naresh

@ppr

Hi @Mandava_Naresh

Please use the following query in the Invoke Code activity:

io_dt.AsEnumerable.ToList.ForEach(Sub(row)
row("Status") = If(row("Status").ToString.ToLower.Equals("failed"),"Error",row("Status").ToString)
End Sub)

Hope this helps,
Best Regards.

@arjunshenoy can’t we use LINQ query thru assign ?

have a look on the different options
How to Update Data Column Values of a Data Table | Community Blog

whenever we want to use a statement within an assign activity we have to ensure, that each part within the statement is returning a value, This was done with the DataTable Reconstruction approach.

setField method / selective data column value update will not return a value. So we use the invoke code

I tried this but it is not working

Initial DT

Invoke code

below result after invoke code → No change in status (Failed is not replaced with error )

→ PFB xaml file and report input file

Rename.xaml (5.7 KB)
Report.xlsx (545.2 KB)

Note : Sometimes we may not have status with Failed … linq may fail in this kind of scenarios ?

is there possibilities that Linq should work incase If we don’t have Failed records in status ?

Hi @Mandava_Naresh

In the query, since you are converting row(“Status”) to lower case using .ToLower, you must be comparing the data with ‘failed instead of ‘Failed’. Please Change it to ‘failed’ & let us know if it works.

Best Regards.

1 Like

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