Copied to clipboard

Flag this post as spam?

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


  • bh 131 posts 410 karma points
    6 days ago
    bh
    0

    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.

  • Louis Jackson-Rees 41 posts 167 karma points
    6 days ago
    Louis Jackson-Rees
    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 131 posts 410 karma points
    6 days ago
    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

  • Louis Jackson-Rees 41 posts 167 karma points
    6 days ago
    Louis Jackson-Rees
    1

    Aplologies, try .Select

  • bh 131 posts 410 karma points
    6 days ago
    bh
    0

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

  • bh 131 posts 410 karma points
    5 days ago
    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