Currently have this:
foreach (var series in Chart1.Series)
{
series.Enabled = false;
}
I would like to express this in a simple, one line expression. I thought this would work:
Chart1.Series.Select( series => series.Enabled = false);
This doesn’t have any effect, however. Presumably because I just misunderstood how Select was working, which is fine.
My next thought was to do something like Chart1.Series.ForEach( series => series.Enabled = false), but Chart1.Series does not implement IEnumberable (..or at least ForEach is not an acceptable method to call).
I’d rather not do Chart1.Series = Chart1.Series.ToList().ForEach( series => series.Enabled = false);, but maybe that is the simplest option?
The
foreachis preferred for what you’re trying to do. You’re iterating over a sequence of elements and modifying the elements. That’s whatforeachis 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.