如何创建扩展方法来处理带有谓词输入的 bindinglist.removeall
myGenericList.RemoveAll(x => (x.StudentName == "bad student"));
效果很好,但是绑定列表没有这个方法。如何为绑定列表创建一个扩展方法,该方法将谓词作为输入,并像列表的罐装删除一样发挥作用,
谢谢
myGenericList.RemoveAll(x => (x.StudentName == "bad student"));
Works great, but a bindinglist does not have this method. How can I create an extension method for the bindinglist that takes as input a predicate and does the magic like the canned removeall for List
thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像我在评论中所说,扩展方法没有什么神奇之处,只需像正常编写代码一样编写代码,只需将其放在静态类中的静态方法中并使用
this
关键字:您必须使用
ToArray()
(或ToList()
),因为Where()
是惰性的,仅在以下情况下枚举集合:需要,并且您无法枚举更改的集合。尽管此解决方案相当慢 (O(N2)),因为每个
Remove()
都必须遍历集合以找到要删除的正确项目。我们可以做得更好:这利用了我们可以在常数时间内到达第 i 项的事实,因此整个方法是 O(N)。迭代更容易向后编写,因此我们尚未考虑的项目的索引不会改变。
编辑:实际上,第二个解决方案仍然是 O(N2),因为每个
RemoveAt()
都必须移动该项目之后的所有项目被删除。Like I said in a comment, there is no magic in extension methods, just write the code the same way as if you wrote it normally, just put it in a static method in a static class and use the
this
keyword:You have to use
ToArray()
(orToList()
), becauseWhere()
is lazy and only enumerates the collection when needed and you can't enumerate changing collection.Although this solution is quite slow (O(N2)), because every
Remove()
has to look through the collection to find the correct item to remove. We can do better:This uses the fact that we can get to i-th item in constant time, so the whole method is O(N). The iteration is easier to write backwards, so that indexes of items we have yet to consider aren't changing.
EDIT: Actually the second solution is still O(N2), because every
RemoveAt()
has to move all the items after the one that was removed.我想说:
对于那些对细节感兴趣的人,似乎 ToList() 和 ToArray() 的性能非常接近(事实上,根据具体情况,每个都可以更快),可以忽略不计: “https://stackoverflow.com/questions/1826658/what-is-faster-or-preferred-ienumerable-toarray-or-tolist">我需要迭代和计数。哪一个最快或首选:ToArray() 还是 ToList()?
I'd say:
And for those interested in the minutia, seems ToList() and ToArray() are so close to the same performance (and in fact each can be faster based on the circumstance) as to be negligible: I need to iterate and count. What is fastest or preferred: ToArray() or ToList()?