代码之家  ›  专栏  ›  技术社区  ›  TechTravelThink

LINQ删除SilverLight子级中的UIElement

  •  0
  • TechTravelThink  · 技术社区  · 14 年前
    foreach (UIElement el in GridBoard.Children.ToList())
    {
       if (el is Ellipse)
       {
           GridBoard.Children.Remove(el);
       }
    }
    

    有没有等效的LINQ来完成上面的工作?如果是,请提供代码?谢谢

    1 回复  |  直到 12 年前
        1
  •  1
  •   Ahmad Mageed    14 年前

    LINQ用于查询集合,而不是导致副作用。根据MSDN,Silverlight不支持 List<T> RemoveAll 方法,但确实支持 Remove RemoveAt 方法,否则您将能够编写: 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);
    }
    

    移除 这将产生一些更好的性能,然后使用 删除

    for (int i = GridBoard.Children.Count - 1; i >= 0; i--)
    {
        if (GridBoard.Children[i] is Ellipse)
            GridBoard.Children.RemoveAt(i);
    }
    

    所以和你的情况没什么不同。也许 支持将使它进入未来的Silverlight版本,这将是最好的选择。

    推荐文章