Update data table using linq

Hello All,

I want to update datatable .It has multiple columns, basis on one column need to update second column using linq.
Can anybody help me with linq ?

Regards,
Akanksha

2 Likes

@akanksha.patil630 - Can you please elaborate?

I have a data table with 50K records. I want to update one column value basis on another column value like shown below.
IF the value of ‘Shift’ Column is ‘MSP’, I want to update ‘Tag_Shift’ Column as ‘MSP-MOB’.

image

1 Like

You can try below steps -

  1. Read the excel into data table
  2. Use for each row and iterate through the items
  3. If you have one condition use if activity and if you have multiple condition then use switch case activity…and then inside these activities update your row value.

Let me know if any issue.

1 Like

@parasmanichauhan solution will work for what you need, if you want to explore using Linq I have provided it below but you will have to adjust it within UiPath.

var rowsToUpdate = 
    Datatable.AsEnumerable().Where(r => r.Field<string>("Shift") == "MSP");

foreach(var row in rowsToUpdate)
{
    row.SetField("Tag_Shift", "MSP-MOB");
}

Or you can use Datatable.Select:

Datatable.Select("Shift='MSP'").ToList().ForEach(Sub(row) row("Tag_Shift")="MSP-MOB")
2 Likes

Thanks for reply @parasmanichauhan .
I am looking for linq as For each loop will take considerable time to execute.

1 Like

Thank you @TimK , appreciate your response.

2 Likes

TimK,
I am trying your suggestion but I am getting a compile error “Expression does not produce a value”. I am trying this is Assign activity with the below suggestion of yours -
Datatable.Select(“Shift=‘MSP’”).ToList().ForEach(Sub(row) row(“Tag_Shift”)=“MSP-MOB”)
Not sure why. Could you help on this?

The reason of this ist that foreach ist a sub Routine and is Not returning a value. A Lot of linq Datatable Update cases can be done, but requires a different Handling.

WE can Help on this, but IT IS recommended to Open a seperate topic, so WE can Adress individually

Thanks @ppr. I have created a new topic for this error -

Hi all ,

I tried this using Invoke Code .FilterLinq.xaml (7.3 KB)

Hi Timk, @jeevith @ClaytonM I want to update two column into for each.

dt.AsEnumerable().Where(Function(row) row (“Available”).ToString.Equals(“Find”)).ToList().ForEach( Sub (row) row(“UpdateCol”) = “Action need”; row(“FF”) = “No Action”)

In for each one column is working fine but when i pass two column it will return error.