How can I remove nodes from a SiteMapNodeCollection?

前端 未结 3 1496
清歌不尽
清歌不尽 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.

    0 讨论(0)
  • 2021-01-24 09:52

    Your shouldn't need CType

    Dim children = _
        From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
        Where n.Url <> "/Registration.aspx" _
        Select n
    
    0 讨论(0)
  • 2021-01-24 10:00

    I got it to work with code below:

    Dim children = From n In SiteMap.CurrentNode.ChildNodes _
                   Where CType(n, SiteMapNode).Url <> "/Registration.aspx" _
                   Select n
    RepeaterSubordinatePages.DataSource = children
    

    Is there a better way where I don't have to use the CType()?

    Also, this sets children to a System.Collections.Generic.IEnumerable(Of Object). Is there a good way to get back something more strongly typed like a System.Collections.Generic.IEnumerable(Of System.Web.SiteMapNode) or even better a System.Web.SiteMapNodeCollection?

    0 讨论(0)
提交回复
热议问题