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);
}
}
是否有任何 LINQ 等效于执行上述操作?如果是,可以提供代码吗?谢谢
LINQ 用于查询集合而不是产生副作用。根据 MSDN Silverlight 不支持List<T>
'RemoveAll
方法,但支持Remove
andRemoveAt
方法,否则您将能够编写:GridBoard.Children.ToList().RemoveAll(el => el is Ellipse);
您可以按如下方式使用 LINQ:
var query = GridBoard.Children.OfType<Ellipse>().ToList();
foreach (var e in query)
{
GridBoard.Children.Remove(e);
}
或者,您可以反向遍历您的列表并使用RemoveAt
它会产生更好的性能,然后使用Remove
:
for (int i = GridBoard.Children.Count - 1; i >= 0; i--)
{
if (GridBoard.Children[i] is Ellipse)
GridBoard.Children.RemoveAt(i);
}
所以它和你所拥有的并没有太大的不同。也许RemoveAll
支持将使其进入未来的 Silverlight 版本,这将是最佳选择。