<p>If I wanted to find checked check boxes on an ASP.NET page I could use the following LINQ query.</p>

<pre><code>var checkBoxes = this.Controls
                     .OfType&lt;CheckBox&gt;()
                     .TakeWhile&lt;CheckBox&gt;(cb =&gt; cb.Checked);
</code></pre>

<p>That works fine if the checkboxes are nested in the current control collection, but I'd like to know how to extend the search by drilling down into the control collections of the top-level controls.</p>

<p>The question was asked here:</p>

<p><a href="https://stackoverflow.com/questions/28642/finding-controls-that-use-a-certain-interface-in-aspnet">Finding controls that use a certain interface in ASP.NET</a></p>

<p>And received non-LINQ answers, I already have my own version of a recursive control search on type and ID as extension methods, but I just wondered how easy this is to do in LINQ?</p>

Take the type/ID checking out of the recursion, so just have a "give me all the controls, recursively" method, e.g.
```
```
public static IEnumerable<Control> GetAllControls(this Control parent)
{
    foreach (Control control in parent.Controls)
    {
        yield return control;
        foreach(Control descendant in control.GetAllControls())
        {
            yield return descendant;
        }
    }
}
```
```
That's somewhat inefficient (in terms of creating lots of iterators) but I doubt that you'll have a
very
deep tree.
You can then write your original query as:
```
```
var checkBoxes = this.GetAllControls()
                     .OfType<CheckBox>()
                     .TakeWhile<CheckBox>(cb => cb.Checked);
```
```
(EDIT: Changed AllControls to GetAllControls and use it properly as a method.)