LINQ 删除 SilverLight Children 中的 UIElement
foreach (UIElement el in GridBoard.Children.ToList())
{
if (el is Ellipse)
{
GridBoard.Children.Remove(el);
}
}
是否有任何 LINQ 相当于执行上述操作?如果是的话,可以提供一下代码吗?谢谢
foreach (UIElement el in GridBoard.Children.ToList())
{
if (el is Ellipse)
{
GridBoard.Children.Remove(el);
}
}
Is there any LINQ equivalent to do the above? If yes, can please provide the code? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LINQ 用于查询集合而不是产生副作用。根据 MSDN Silverlight 不支持
List
的RemoveAll
方法,但支持Remove
和RemoveAt
> 方法,否则您可以编写:GridBoard.Children.ToList().RemoveAll(el => el is Ellipse);
您可以按如下方式使用 LINQ:
或者,您可以反向遍历您的列表并使用
RemoveAt
,这会比使用Remove
产生更好的性能:所以它与您所拥有的没有太大不同。也许
RemoveAll
支持将进入未来的 Silverlight 版本,并且它将是最佳选择。LINQ is used to query collections rather than cause side-effects. According to MSDN Silverlight doesn't support
List<T>
'sRemoveAll
method but does support theRemove
andRemoveAt
methods, otherwise you would've been able to write:GridBoard.Children.ToList().RemoveAll(el => el is Ellipse);
You could use LINQ as follows:
Alternately, you could traverse your list in reverse and use
RemoveAt
which would yield some better performance then usingRemove
:So it's not much different than what you had. Perhaps
RemoveAll
support will make it's way into future Silverlight versions and it would be the best choice.