Set a property for each item in the collection

I have an IEnumerable, and I'm trying to call a "Text" setter in every element of an enumerable. I can do:

foreach (Data d in DataEnumerable) { d.Text="123"; } 

I can also do:

 DataEnumerable.All(x => { x.Text = "123"; return true; }); 

What is the best practice of calling the set method for each element of an enum?

+4
source share
1 answer

The first method is better.

The second is the abuse of Enumerable.All . This method is designed to test all Enumerable elements to ensure that they satisfy the condition. You do not do this.

There is a List.ForEach method that can be used for this type of update operation, but the LINQ team decided not to add the corresponding method to Enumerable . See Eric Lippert's Blog for details on why this decision was made:

+10
source

All Articles