Update cell with linq

Hello everyone

I need to update the excel cells that meet certain conditions, for example, I have the code column and the batch column, if the value of the code column is equal to “CM84” and the value of the batch column begins with “CM” I need that the value of the code column changes and is “CM28”

I solved this with a for each in data table, but due to the amount of data that I have in the file it is not the best solution, that is why I am trying to solve it with a linq

hi there, you can try this.

Instead of updaing column C, you can update column B.

image

dt = dt.AsEnumerable.Select(Function(r) dt.Clone.LoadDataRow({r(“Col A”),r(“Col B”),if(r(“Col A”).ToString.Contains(“Needed”) AndAlso r(“Col B”).ToString.ToUpper=“TRUE”,“Present”,r(“Col C”)) ,r(“Col D”)},False)).CopyToDataTable

Hello, thank you very much, I had already seen these posts, but in the first one that you shared with me I have not been able to change only column B, that script always changes column C and only selects columns from A to D, I need that the file remains intact and only the cells that meet the conditions are changed (the file contains about 50 columns)

LINQ doesn’t avoid looping. It still loops inside the LINQ engine. It’s a myth that LINQ is faster. In fact, LINQ can be slower because it introduces more overhead.

1 Like

Hi @Juan_Esteban_Valencia

Have a look on video

Regards
Gokul

(From r In DT_Data.AsEnumerable Let ra = r.ItemArray.Select(Function (x) x.ToString.Trim.Replace(" ",“NA”)).toArray() Select DT_Data.Rows.Add(ra)).CopyToDataTable()

you can try this