Rick van den Bosch - Blog

... on software development, architecture and more

Selecting all the controls of a specific type, the (built in!) LINQ way

We probably all worked with dynamically generated controls on forms, ASP.NET pages or (user) controls. And I guess we've all written for-each statements to loop through the ControlCollection and filter out all the controls of a specific type, right? I wanted to do the same today, so I started by trying to use LINQ for this:
linq_OfType_1 
This, as you can see, generated a compile time error:
Could not find an implementation of the query pattern for source type 'System.Web.UI.ControlCollection'.  'Where' not found.  Consider explicitly specifying the type of the range variable 'control'.

As the error states, this is easily solved by explicitly specifying the type of the control variable. Because a ControlCollection can contain so many types, it is not strange the compiler asks you to be a bit more precise on the type you will be querying. That looks something like this:
linq_OfType_2

But when I added the System.Linq namespace to my using section, I saw an extra method on the ControlCollection, called OfType<>. This method is one of the extension methods that can be applied to a collection that has a non-parameterized type like a ControlCollection or an ArrayList, because OfType<> extends the type IEnumerable.
Another one of those nice 'native' Linq extension methods is the Cast<> method, which pretty much does the same but throws an exception if there are any objects in the collection that cannot be cast to the specified type. And yes, exceptions are sometimes wanted ;)

For more info on the OfType<> method, see: Enumerable.OfType<TResult> Generic Method
For more info on the Cast<> method, see: Enumerable.Cast<TResult> Generic Method


Technorati tags: ,,

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 18, 2008 7:02 PM

Miron said:

Hi Rick,

The code on your post will not collect controls that inside other controls (TextBox inside FormView etc...)

To really collect all the controls from a specified type you need to run recursive method.

Take a look at the solution:

mironabramson.com/.../Collect-all-controls-from-specific-type.aspx

# April 19, 2008 1:31 AM

Rick van den Bosch said:

Hi Miron,

You are right, nested controls will not be found. But to be honest, that wasn't what I was trying to achieve ;)

The intention of my post was to bring the new .OfType<> method under the attention. I guess that worked :)

Anyway, thanks for your comment. People who are looking for that functionality and end up here now have a link to a post with code that does exactly that.

Thanks again!

Kind regards,

Rick

# April 19, 2008 9:29 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 8 and 7 and type the answer here: