Question

Linq find all with certain type

Is there a way to find in a List all items of a certain type with a Linq/Lambda expression?

Update: Because of the answers, I realize the question wasn't specific enough. I need a new List with only the items of a specific type. In my case, the class hasn't got any subclasses, so no need to take inheritance into account.

 45  40413  45
1 Jan 1970

Solution

 120

Use OfType<T> like so:

foreach (var bar in MyList.OfType<Foo>()) {
    ...
}
2010-07-13

Solution

 39

Will this do?

list.Where(t => t is MyType);
2010-07-13