How can I update LINQ all records that have the same value?

Can someone help me with this: I want to select all records from datatable that have, for example, sid = 123, and after that save them using sid = 456.

How to do this with LINQ?

+3
source share
1 answer
items.Where(i=>i.sid == 123).ToList().ForEach(i=>i.sid = 456);

or rather use regular foreach

foreach (var item in items.Where(i=>i.sid == 123))
{
    item.sid = 456
}

Edit: Sorry, I did not notice that it is datatable. you cannot directly query rows in datatable (they do not execute IEnumerable)

but you can do something like this

using System.Data; //System.Data.DataSetExtensions.dll
datatable.AsEnumerable().Where(row=>row.Field<int>("sid") == 1234)
+8
source

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


All Articles