Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OfType ForEach exclude particular elements

Tags:

c#

foreach

oftype

I have a line of code that clears all labels from the form

Controls.OfType<Label>().ToList().ForEach(p => p.Visible = false);

Now I need to exclude particular labels(either by id or text) from that list (like title labels). Is there a way to do so by modifying that line of code alone? I found out about Where(), though I am not sure about the syntax that goes inside those brackets.

like image 563
Vadzim Savenok Avatar asked Sep 20 '26 05:09

Vadzim Savenok


1 Answers

I believe you want something like this:

Controls.OfType<Label>().Where(lbl => lbl.Title != "something").ToList().ForEach(p => p.Visible = false);
like image 128
Camilo Terevinto Avatar answered Sep 21 '26 17:09

Camilo Terevinto