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 don't want my registration form page to show up there.

Dim Children As SiteMapNodeCollection = SiteMap.CurrentNode.ChildNodes

'remove registration page from collection
For Each n As SiteMapNode In SiteMap.CurrentNode.ChildNodes
If n.Url = "/Registration.aspx" Then
End If

RepeaterSubordinatePages.DataSource = Children

The SiteMapNodeCollection.Remove() method throws a NotSupportedException: "Collection is read-only".

How can I remove the node from the collection before DataBinding the Repeater?

+1  A: 

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.


I got it to work with this syntax:

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?

Zack Peterson
+1  A: 

Your shouldn't need CType

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

Perfect! Thank you Keith!

Zack Peterson