简体   繁体   中英

Select items from a collection based on the sub-class type in the collection

class A {}
class B : A {}
class C : A {}

ICollection<A> myCollection;
var myresults = myCollection.Where(item => item.GetType() is C);

Given the hierarchy above the where predicate effectively does nothing.

How can I structure the where predicate to return all items of type C from the collection?

而不是在哪里可以使用OfType<TResult>

var myresults = myCollection.OfType<C>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM