Briskstar Technologies
Briskstar Technologies

Reputation: 2253

Update multiple rows in datatable without loop

I have two datatable with conditions in application and want some processing for multiple rows to update column value.

For example:

I have datatable1 with 10000 rows. I want to filter rows by datatable.select("condition") and as per condition, I want to update row values.

If for any condition, I found 20 rows from datatable. I want to update those 20 records in one shot. Not in any loop. I have datarow array for those values to update in datable.

Upvotes: 2

Views: 40921

Answers (4)

An example in VB.NET

dt.Select("id = 1").ToList().ForEach(Sub(drow) drow("val") = "number 1")

Upvotes: 0

Sarath Subramanian
Sarath Subramanian

Reputation: 21301

If you want to default a column value with abc use Expression, then you could use the below code.

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

In case if you need to pass the value dynamically using a variable, you could use the below code.

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

Upvotes: 0

sivabalan
sivabalan

Reputation: 159

To Update Row With Multiple Condition use This

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

Upvotes: 2

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

You can try out 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 substitute your columns and values here...

Upvotes: 11

Related Questions