How can I remove nodes from a SiteMapNodeCollection?

坚强是说给别人听的谎言 提交于 2019-12-02 05:43:26

Your shouldn't need CType

Dim children = _
    From n In SiteMap.CurrentNode.ChildNodes.Cast(Of SiteMapNode)() _
    Where n.Url <> "/Registration.aspx" _
    Select n

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.

Zack Peterson

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!