Linq查找具有特定类型的所有对象 [英] Linq find all with certain type

查看:54
本文介绍了Linq查找具有特定类型的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在列表中查找具有Linq/Lambda表达式的特定类型的所有项目?

Is there a way to find in a List all items of a certain type with a Linq/Lambda expression?

更新:由于有了答案,我意识到这个问题不够具体.我需要一个仅包含特定类型项目的新列表.就我而言,该类没有任何子类,因此无需考虑继承.

Update: Because of the answers, I realize the question wasn't specific enough. I need a new List with only the items of a specific type. In my case, the class hasn't got any subclasses, so no need to take inheritance into account.

推荐答案

使用OfType<T>就像这样:

foreach (var bar in MyList.OfType<Foo>()) {
    ...
}

这篇关于Linq查找具有特定类型的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆