How can I remove nodes from a SiteMapNodeCollection?

前端 未结 3 1587
清歌不尽
清歌不尽 2021-01-24 09:14

I\'ve got a Repeater that lists all the web.sitemap child pages on an ASP.NET page. Its DataSource is a SiteMapNodeCollection. But, I do

3条回答
  •  死守一世寂寞
    2021-01-24 09:48

    Using Linq and .Net 3.5:

    //this will now be an enumeration, rather than a read only collection
    Dim children = SiteMap.CurrentNode.ChildNodes.Where( _
        Function (x) x.Url <> "/Registration.aspx" )
    
    RepeaterSubordinatePages.DataSource = children 
    

    Without Linq, but using .Net 2:

    Function IsShown( n as SiteMapNode ) as Boolean
        Return n.Url <> "/Registration.aspx"
    End Function
    
    ...
    
    //get a generic list
    Dim children as List(Of SiteMapNode) = _
        New List(Of SiteMapNode) ( SiteMap.CurrentNode.ChildNodes )
    
    //use the generic list's FindAll method
    RepeaterSubordinatePages.DataSource = children.FindAll( IsShown )
    

    Avoid removing items from collections as that's always slow. Unless you're going to be looping through multiple times you're better off filtering.

提交回复
热议问题