Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robin Hansen 135 posts 368 karma points
    Aug 01, 2014 @ 13:11
    Robin Hansen
    0

    Exclude nodes by nodeid in foreach loop

    I need to exclude a set of nodes by their id in a foreach loop...

    I call my id's like this:

    string excludeId = "";
    foreach (var item in resultingString)
    {
    excludeId += @item.Id + ", ";
    }
    var ids = excludeId.Substring(0, excludeId.LastIndexOf(","))
    var childNodes = startNode.DescendantsOrSelf(4).OrderBy("UpdateDate desc").Where(x => x.Id != ids);
    @foreach (var page in childNodes.Take(1))
    {

    @page.Name


    }

    How Do I filter out the comma-separated list of nodeid's from childNodes...?

    NOTE: Making an if-sentence inside the foreach-loop is not an option...!

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 01, 2014 @ 13:15
    Jeavon Leopold
    0

    Hi Robin,

    Could you please post a little more of your code (especially the first line) so we can work out what startNode is and what type of Razor you are using?

    Jeavon

  • Robin Hansen 135 posts 368 karma points
    Aug 01, 2014 @ 13:23
    Robin Hansen
    0

    Certainly :)

    @inherits Umbraco.Web.Macros.PartialViewMacroPage    
    string excludeId = "";
    foreach (var item in resultingString)
    {
       excludeId += @item.Id + ", ";
    }
    var ids = excludeId.Substring(0, excludeId.LastIndexOf(","))
    
    var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);**
    var childNodes = startNode.DescendantsOrSelf(4).OrderBy("UpdateDate desc").Where(x => x.Id != ids);
    @foreach (var page in childNodes.Take(1))
    {   
        @page.Name
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 01, 2014 @ 13:34
    Jeavon Leopold
    101

    Ok, cool, so the main issue is that you are trying to do a lambda on a dynamic object, how about something like this (requires Umbraco v6.2+ or v7.1.2+)?

    var excludesId = "1234,5678,2332";
    
    var startNode = Umbraco.TypedContent(2323);
    
    var childNodes = startNode.DescendantsOrSelf(4).Where(x => !x.Id.ToString().CsvContains(excludesId)).OrderBy("UpdateDate desc");
    foreach (var page in childNodes.Take(1))
    {
        @page.Name
    }
    
  • Robin Hansen 135 posts 368 karma points
    Aug 01, 2014 @ 13:41
    Robin Hansen
    0

    This is typical when I try to write Lambda:

    "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
    

    :(

    (I'm using Umbraco v. 7.1.4 by the way...)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 01, 2014 @ 13:58
    Jeavon Leopold
    0

    Did you try the above snippet I posted?

  • Robin Hansen 135 posts 368 karma points
    Aug 01, 2014 @ 14:38
    Robin Hansen
    0

    Yes but I cannot use TypedContent:

    var startNode =Umbraco.TypedContent(2323);

    I need to use :

    var startNode =Umbraco.Content(Model.MacroParameters["startNodeID"]);

    How ever - the Lambda expression does not filter out any nodes as I whant to - I still se some certain nodes as should not be visible...

    It will only sort on 1 (one) id - not a comma serpareted list of Ids

     

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 01, 2014 @ 14:40
    Jeavon Leopold
    0

    You need to use it otherwise you cannot use a lambda.

    Like this:

    var startNode = Umbraco.TypedContent(Model.MacroParameters["startNodeID"].ToString());
    
  • Robin Hansen 135 posts 368 karma points
    Aug 01, 2014 @ 14:50
    Robin Hansen
    0

    I'll give it a try - about the sorting - it seems that it will only sort on 1 id - not a list of ids... :|

  • Robin Hansen 135 posts 368 karma points
    Aug 04, 2014 @ 11:20
    Robin Hansen
    0

    This one nailed it...: :)

    var startNode = Umbraco.TypedContent(Model.MacroParameters["startNodeID"].ToString());
    var excludedId = "";
    foreach (var item in resultingString.OrderBy(x => x.Id))
    {
       excludedId += @item.Id + ", ";
    }
    List<int> excludedAppIds = excludedId.Substring(0, excludedId.LastIndexOf(",")).Split(',').Select(x => x.Trim()).Select(x => Int32.Parse(x)).ToList();
    var childNodes = startNode.DescendantsOrSelf(4).Where(i => !excludedAppIds.Contains(i.Id));
Please Sign in or register to post replies

Write your reply to:

Draft