Search code examples
c#umbracoumbraco7

How to exclude a single node id from a loop of CurrentPage.Children


I have a template where I wish to display all of the CurrentPage's childnodes except a single node with Id = 1234.

My current code:

 @{
   foreach( var child in CurrentPage.Children.Take(6) ){

   Html.Partial("Archive/Post", new ViewDataDictionary {{ "item", (object)child }} )

        }
   }

So basically I'm trying to do something like: CurrentPage.Children.Take(6).Where(Id != 1234)


Solution

  • The Where method expects a predicate (usually provided as a lambda expression):

    CurrentPage.Children.Take(6).Where(c => c.Id != 1234)
    

    Following your comment, I came to the conclusion that either CurrentPage is dynamic or Childern is dynamic. Since you can't use lambda expressions with dynamic, You can go the old fashion way and simply add the condition inside the loop:

    @{
        foreach( var child in CurrentPage.Children.Take(6) )
        {
          if(child.Id != 1234)
              Html.Partial("Archive/Post", new ViewDataDictionary {{"item", (object)child}});
        }
    }