简体   繁体   English

根据集合中的子类类型从集合中选择项目

[英]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? 如何构造where谓词以返回集合中所有C类型的项目?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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