views:

42

answers:

2

If I have a collection like

List<SomeAbstractBaseType> someList = new List<SomeAbstractBaseType>();

And I have added two different child types to this collection (i.e. the two child types inherit from SomeAbstractBaseType) like this:

someList.Add(someChildOfType1);
someList.Add(someChildOfType2);
someList.Add(someOtherChildOfType1);
someList.Add(someOtherChildOfType2);

Say I want to query for all elements of someChildOfType1. How can I do this I have tried the following (which doesn't compile, can covert from child type to parent type)

List<ChildType1> temp = someList.Where(x => x.GetType() == typeof(ChildType1)).ToList();

Any ideas how I could do this?

A: 

Enumerable.OfType<T>() extension method.

leppie
+3  A: 
List<ChildType1> temp = someList.OfType<ChildType1>().ToList();
Leom Burke