I have seen a method somewhere here on SO to restrict items returned from a foreach loop to a certain type, using, if I remember correctly, LINQ extensions on IEnumerable and a lambda for the type check. I can’t find it again, can anyone suggest how this was achieved?
Share
You could do this with a call to the Where extension method, as follows:
But this is so useful that it is built into the .NET framework, and that is what Chris is pointing out above. IEnumerable has an extension method called OfType, documented here.
To use it, you’d do something like this: