Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq find all with certain type

Tags:

c#

linq

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.

like image 550
Lieven Cardoen Avatar asked Jul 13 '10 11:07

Lieven Cardoen


1 Answers

Use OfType<T> like so:

foreach (var bar in MyList.OfType<Foo>()) {     ... } 
like image 111
Dave Markle Avatar answered Sep 24 '22 12:09

Dave Markle