title: LINQ selection by type of an object
body: <p>I have a collection which contains two type of objects A &amp; B.</p>
  <pre><code>Class Base{}
  Class A : Base {}
  Class B : Base {}
  
  List&lt;Base&gt; collection = new List&lt;Base&gt;();
  collection.Add(new A());
  collection.Add(new B());
  collection.Add(new A());
  collection.Add(new A());
  collection.Add(new B());
  </code></pre>
  <p>Now I want to select the objects from the collection based on its type (A or B, not both).</p>
  <p>How I can write a LINQ query for this? Otherwise I need to loop through the collection, which I don't want to.</p>
  <p>Edit:</p>
  <p>Thank you all for your help. Now I can use OfType() from LINQ. But I think in my case it won't work. My situation is</p>
  <pre><code>Class Container
  {
    List&lt;Base&gt; bases;
  }
  
  List&lt;Container&gt; containers = new List&lt;Container&gt;();
  </code></pre>
  <p>Now I want to select the container from containers, which has at least one type A. Maybe this can't be done by LINQ.</p>