fo开发者_StackOverflow社区reach (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
LINQ is used to query collections rather than cause side-effects. According to MSDN Silverlight doesn't support List<T>
's RemoveAll
method but does support the Remove
and RemoveAt
methods, otherwise you would've been able to write: GridBoard.Children.ToList().RemoveAll(el => el is Ellipse);
You could use LINQ as follows:
var query = GridBoard.Children.OfType<Ellipse>().ToList();
foreach (var e in query)
{
GridBoard.Children.Remove(e);
}
Alternately, you could traverse your list in reverse and use RemoveAt
which would yield some better performance then using Remove
:
for (int i = GridBoard.Children.Count - 1; i >= 0; i--)
{
if (GridBoard.Children[i] is Ellipse)
GridBoard.Children.RemoveAt(i);
}
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.
精彩评论