Hi,
I have a collection which contains two type of objects A & B.
Class Base{}
Class A : Base {}
Class B : Base {}
List<Base> collection = new List<Base>();
collection.Add(new A());
collection.Add(new B());
collection.Add(new A());
collection.Add(new A());
collection.Add(new B());
Now I want to select the objects from the collection based on its type (A or B, not both).
How I can write a LINQ query for this? Please help me. Otherwise I need to loop through the collection, which I dont want to. Thanks.
Edit:
Thank you all for your help. Now I can use OfType() from LINQ. But I think in my case it wont work. My situation is
Class Container
{
List<Base> bases;
}
List<Container> containers = new List<Container>();
Now I want to select the container from containers, which has at least one type A. May be this cant be done by LINQ. Thanks a lot.