Updating multiple rows in datatable without loop

I have two types of data with conditions in the application and it needs some processing for several rows to update the column value.

For instance:

I have datatable1 with 10,000 rows. I want to filter rows using datatable.select ("condition"), and according to the condition I want to update the values ​​of the rows.

If for any condition I found 20 rows from a datatable. I want to update these 20 entries in one shot. Not in any cycle. I have a datarow array for these values ​​to update in a datable.

+1
source share
3 answers

You can try the following linq,

DataTable recTable = new DataTable(); // do stuff to populate table recTable.Select(string.Format("[code] = '{0}'", someName)).ToList<DataRow>().ForEach(r => r["Color"] = colorValue); 

You can replace your columns and values ​​here ...

+5
source

To update a string using several conditions This

  datatable.Select(string.Format("[lineNo]='{0}' and [Position]>='{1}' ", lineNo, Position)).ToList<DataRow>().ForEach(r => r["Linetext"] ="Sample Text" ); 
+1
source

If you want the default column value with abc use Expression , you can use the code below.

 dt.Columns.Add("ColumnName").Expression = "'abc'"; 

If you need to pass the value dynamically using a variable, you can use the code below.

 string str = "abc"; dt.Columns.Add("ColumnName").Expression = "'" + str + "'"; 
0
source

Source: https://habr.com/ru/post/1237467/


All Articles