How to replace a specific cell from datatable using linq

I refered few forum questions and got a linq to replace specific value using .replace

So i wrote this linq code
(From row In dt_B
row(“City”) = If (row(“City”).toString.ToUpper.Trim.Equals(“KOLKATA”),“Error”,row(“City”).toString))

But i got error as
image

Hi @Kira ,

Could you also point out to the referred Forum links here, so that we can analyse or compare and see what you have missed out. Maybe a thorough check again on those links would help you to resolve it.

But notice that there are some specific differences to keep in mind when it comes to updating using Linq.
In terms of Datatable update and with Assign activities, a recreation of the Datatable with the updated values need to take place.

We can also update the same reference or variable using Invoke Code activity and calling the methods on the datatable reference variable.

More on this topics you could find below :

Hi @Kira As mentioned in the previous reply if you can mention your reference we can see what is the difference. From what i understand you are trying to check if any of the cell value matches “Kolkata” if yes replace it, the below expression if given inside an ‘invoke code’ activity will do that task, just mentioning something i would do for quick output, there are other options as well.

Dt_Book.AsEnumerable().ToList().ForEach(Sub(row) row(“Employee Name”) = If(row(“City”).ToString().ToUpper().Trim().Equals(“KOLKATA”), “Error”, row(“Employee Name”).ToString()))

Hi @Kira

Try this query:

(From row In dt_B.AsEnumerable()
 Let cityValue = row("City").ToString().ToUpper().Trim()
 Let newValue = If(cityValue.Equals("KOLKATA"), "Error", cityValue)
 Select dt_B.Clone().Rows.Add(row.ItemArray.Take(row.ItemArray.Length - 1).Concat({newValue}).ToArray())).CopyToDataTable()

Hope it helps

Hey @Kira ,

You can try below code

(From row In dt_B
Let cityValue = If(row("City").tostring.toUpper.Trim.Equals("KOLKATA"),"Error",row("City").tostring)
Select dtCity.Clone.Rows.add({cityValue,(row(1))})
).copyToDatatable

I tried using my own example
So below is my input Datatable
image

Below is how i assigned it

Below is my output
image

Hope it helps you out

1 Like

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