Linq query for updating a value in a datatable based on a condition

I have a datatable dt
There is a column named status
if value of status= “-” need to update the status column as completed

Could anyone please help me with a LINQ query. I can do the same with for loop but i need query in LINQ

Hi @muhamed.fasil

How about this expression

(From row In dt.AsEnumerable()
Let modCol = row.Field(Of String)("Status").ToString.Replace("-","completed")
Let ra = row.ItemArray.Skip(1).Prepend(modCol).ToArray()
Select Dt1.Rows.Add(ra)).CopyToDataTable()

Regards
Gokul

Hi gokul,
do i need to clone Dt to create DT1? u have mentioned a datatable variable in the last SELECT query

I am not able to use the query directly in code , getting error as the Dt1 was not declared

Hi @muhamed.fasil

Check Out the XAML file

UpdateStatus.xaml (8.8 KB)

Output

image

Regards
Gokul

@Gokul001

I have more than 1 column in my datatable, no of columns may vary w.r.t input excel

For example see the input sheet below

image

HI @muhamed.fasil

Check Out the update XAML file

UpdateStatus.xaml (11.0 KB)

Regards
Gokul

@Gokul001

Bro u trying to update the first column ?, but i need to update the column named “status”

To check that can you switch the columns in build datatable and check the output

Share the Input @muhamed.fasil

For me this the Output @muhamed.fasil

image

if i switch the positions of input datatable columns , still the updation is happening on Column1 itself
source.xlsx (9.0 KB)

Hi @muhamed.fasil

Check Out the Updated XAML file

UpdateStatus.xaml (13.2 KB)

Output

source.xlsx (9.7 KB)

Regards
Gokul