Replacing foreach loop with LINQ






4.33/5 (6 votes)
For replacing foreach loop, for a non-query operation, just for performing an operation on each element in the collection, we can use the ForEach method, like the following:students.ToList().ForEach(s => { s.Grade += 10;});
For replacing foreach
loop, for a non-query operation, just for performing an operation on each element in the collection, we can use the ForEach
method, like the following:
students.ToList().ForEach(s =>
{
s.Grade += 10;
});