如何解决在接口的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"));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。