I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me.
I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax
var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault();
However from what I can tell, Controls
does implement the IEnumerable
interface which provides such methods, so what gives? Why doesn't that just work? I have found a decent work around for this issue at least:
var list = (IEnumerable<Control>)Controls;
var this_item = list.Where(x => x.ID == "Some ID").SingleOrDefault();
This is just because the
ControlCollection
class came around before generics; so it implementsIEnumerable
but notIEnumerable<Control>
.Fortunately, there does exist a LINQ extension method on the
IEnumerable
interface that allows you to generate anIEnumerable<T>
through casting:Cast<T>
. Which means you can always just do this:In addition to the answers provided by Jon Skeet and Dan Tao, you can use query expression syntax by explicitly providing the type.
Linq utilized Generic Collections. ControlsCollection implements
IEnumerable
notIEnumberable<T>
If you notice this will not work
However, this does
You can either cast to Generic
IEnumerable<T>
or access an extension method that does, like so:No,
IEnumerable
doesn't have many extension methods on it:IEnumerable<T>
does. They are two separate interfaces, althoughIEnumerable<T>
extendsIEnumerable
.The normal LINQ ways of converting are to use the
Cast<T>()
andOfType<T>()
extension methods which do extend the nongeneric interface:The difference between the two is that
OfType
will just skip any items which aren't of the required type;Cast
will throw an exception instead.Once you've got references to the generic
IEnumerable<T>
type, all the rest of the LINQ methods are available.