Linq筛选器数组,它包含从基类型继承的多个类型



我有一个名为A的基类和两个子类BC:

class B : A
{
    public string Description { get; set; }
}
class C : A
{
    public string AnotherProperty { get; set; }
}

我从一个方法中得到一个数组,该方法返回BC的数组如果B.Description == "some text";

请帮助我如何做到这一点。

List<A> list = ....
var query = list.OfType<B>().Where(b => b.Description != "some text");

如果你还想要所有不是B:

var query = list.Where(a => !(a is B) || ((B)a).Description != "some text"));

最新更新