Copied to clipboard

Flag this post as spam?

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


  • bh 444 posts 1544 karma points
    Dec 05, 2018 @ 14:22
    bh
    0

    Node Query ?

    My tree looks like this:

    • Home
    • Multifamily
    • -MF Child A
    • -MF Child B
    • -MF Child C
    • Commercial Mixed Use
    • -CMU Child A
    • -CMU Child B
    • -CMU Child C

    I want to get all children of Multifamily and Commercial Mixed Use sorted by name. My code is only returning Children from Multifamily. I know why (the First), but I don't know how to fix it.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{    
        var root = Model.Content.Site();
        var selection = root.Children.First(x => (x.Name == "Multifamily" || x.Name == "Commercial Mixed Use")).Children.Where(x => x.IsVisible()).Where(x => x.GetPropertyValue<Boolean>("communityDetailIsHidden") == false).Where(x => x.GetPropertyValue<String>("communityDetailStatus").ToString().ToLower() != "past community");
    }
    

    This is on a Partial Template if that makes any difference.

  • louisjrdev 107 posts 344 karma points c-trib
    Dec 05, 2018 @ 14:37
    louisjrdev
    1

    Hi bh,

    There is only a minor change you should need to make:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{    
        var root = Model.Content.Site();
        var selection = root.Children
        .Where(x => (x.Name == "Multifamily" || x.Name == "Commercial Mixed Use"))
        .Children.Where(x => x.IsVisible())
        .Where(x => x.GetPropertyValue<Boolean>("communityDetailIsHidden") == false)
        .Where(x => x.GetPropertyValue<String>("communityDetailStatus").ToString().ToLower() != "past community");
    }
    

    You just needed to change .First to .Where

    If you were meaning to return only 1 node then at the end add .FirstOrDefault() like so:

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @{    
            var root = Model.Content.Site();
            var selection = root.Children
            .Where(x => (x.Name == "Multifamily" || x.Name == "Commercial Mixed Use"))
            .Children.Where(x => x.IsVisible())
            .Where(x => x.GetPropertyValue<Boolean>("communityDetailIsHidden") == false)
            .Where(x => x.GetPropertyValue<String>("communityDetailStatus").ToString().ToLower() != "past community").FirstOrDefault();
        }
    
  • bh 444 posts 1544 karma points
    Dec 05, 2018 @ 14:46
    bh
    0

    @LouisJackson-Rees thanks for the thought. Unfortunately using Where in place of First won't let me do the .Children.Where that comes next.

    Here's the exact error. 'Umbraco.Web.PublishedContentExtensions.Children

  • louisjrdev 107 posts 344 karma points c-trib
    Dec 05, 2018 @ 15:10
    louisjrdev
    1

    Aplologies, try .Select

  • bh 444 posts 1544 karma points
    Dec 05, 2018 @ 17:18
    bh
    0

    @LouisJackson-Rees thanks. But same error. Umbraco.Web.PublishedContentExtensions.Children

  • bh 444 posts 1544 karma points
    Dec 05, 2018 @ 18:01
    bh
    0

    GOT IT! I did two queries and unioned 'em. @LouisJackson-Rees thanks for your input!

    var root = Model.Content.Site();
        var selection = root.Children.First(x => x.Name == "Multifamily")
            .Children.Where(x => x.IsVisible()).Where(x => x.GetPropertyValue<Boolean>("communityDetailIsHidden") == false)
            .Where(x => x.GetPropertyValue<String>("communityDetailStatus").ToString().ToLower() != "past community");
        var selection2 = root.Children.First(x => x.Name == "Commercial Mixed Use")
            .Children.Where(x => x.IsVisible()).Where(x => x.GetPropertyValue<Boolean>("communityDetailIsHidden") == false)
            .Where(x => x.GetPropertyValue<String>("communityDetailStatus").ToString().ToLower() != "past community");
        selection = selection.Union(selection2);
        selection = selection.OrderBy(x => x.GetPropertyValue("communityDetailCommunityName"));
    
Please Sign in or register to post replies

Write your reply to:

Draft