score:6

Accepted answer

the foreach is preferred for what you're trying to do. you're iterating over a sequence of elements and modifying the elements. that's what foreach is for.

linq is used to take one sequence of elements and generate a new sequence based on some criteria/transformation. not what you're after.

score:0

your first foreach loop is pretty clear and it works, so why need to replace it with linq, it wont be more clear or faster.

otherwise i dont think it will be simpler than what you say above

chart1.series.tolist().foreach( series => series.enabled = false);

score:0

as to the specific reason your select didn't work, bear in mind that select returns an ienumerable<t>, which is an object that knows how to enumerate. in other words, you have a reference to something that can enumerate, but that hasn't yet been enumerated. so if you take this:

var series = chart1.series.select( s => s.enabled = false);
foreach (var s in series) {}

you'll get the effect you intended, because now you're enumerating the series and therefore calling the select delegate. this is rather non-intuitive, of course, which is why select is typically not used in this way (otherwise every time you enumerate, you'll have side effects and enumerating more than once would apply the side effects again.)

score:1

simple and one line :)

foreach (var series in chart1.series) { series.enabled = false; }

More Query from same tag