I have a collection of different objects that derive from the same parent.
How can I extract objects of a particular type from a collection containing mixed types
e.g.
public class A {}
public class B : A {}
public class C : A {}
The collection will contain objects of type B and C
I’m halfway there just need help filling in the ‘[]’ bit
var x = from xTypes in xCollection where '[type of object is type B]' select xTypes;
Thanks.
You should use the
OfType<T>extension method rather than LINQ query syntax for this:This will give you back an
IEnumerable<B>. If you do want to use the LINQ query syntax, you’d have to do this: