Monday, December 15, 2014

C#: Update specific column(s) of the generic list with one value in different ways (Without iteration)

C#: Update one value to specific column(s) of the generic list in many ways:

1. Update column of each item of the list using Select() Clause: 

collection.Select(c => {c.PropertyToSet = value; return c;}).ToList();

Here, The ToList() is needed in order to evaluate the select immediately 
due to lazy evaluation. 
 

2.Update list using ForEach(): 

collection.Where(c => IdsList.Contains(c.PkId).ToList().ForEach(cc => cc.PropertyToSet = "Updated Value");