在接口的ObservableCollection上使用linq查询?

问题描述

我有一个实现接口的类:

class MyClass : IMyInterface
{
    public Name { get; set;}

    // bunch of other stuff...
}

在我的代码中,我想从“收藏夹”中删除一项:

ObservableCollection<IMyInterface> MyCollection = new ObservableCollection<IMyInterface>();
// fill collection and do some other stuff...

// Trying to remove one item based on the Name property of the object. 
// At this point I already kNow,that my collection of IMyInterface is actually a
// collection of MyClass
MyCollection.Remove(c => c.Name == "SomeName");

这给了我以下错误

无法将lambda表达式转换为类型“ IMyInterface”,因为它不是委托类型

有没有办法在这样的接口集合上使用linq表达式?

后续问题:

如果接口中不存在Name属性(因此它仅出现在MyClass中),实现上述目标的方法是什么?我用不同的类型进行了测试(将整个集合投射到ObservableCollection<MyClass>,并在linq查询中进行类型转换),但是效果并不理想。

解决方法

remove方法需要IMyInterface类型的对象。 您应该这样删除项目:

# np.ceil(data['ship_distance'])
data['ship_distance'] = data['ship_distance'].apply(lambda X: int(math.ceil(X)))

print(data)

跟进回应:

如果MyClass上的另一个属性不在接口上,则需要进行一些转换:

MyCollection.Remove(MyCollection.FirstOrDefault(c => c.Name == "SomeName"));