How To Update Items In List<T> Using LINQ



TODO:

You have a list of objects, and you want to update all items with a particular value, based off of a particular condition.

 

SOLUTION:

string newAccountNumber ="12345";
string previousAccountNumber = "54321";

foreach (var account in dsAccountList.FindAll(a => a.AccountNumber == previousAccountNumber))
{
     account.AccountNumber = newAccountNumber;
}

 

NOTES:

The example will update the account number for all accounts with AccountNumber = previousAccountNumber