Copied to clipboard

Flag this post as spam?

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


  • Lucy Foster 10 posts 82 karma points
    Oct 25, 2017 @ 14:47
    Lucy Foster
    0

    Combine two multinode-treepickers

    Hi All,

    Is it possible to combine the results of two multinode-treepickers and put them into a string?

    I have a foreach statement to show some page results, which works but in certain cases I need to filter the results down further.

    I have two variables which gets the selected filter and then a foreach statement with the results, inside here there are two multinode-treepickers and I need to set and if statement to match the string of the multinode-treepickers and the variables.

    But I'm not sure if it's possible to bring the two multinode-treepicker results into a string.

    Thanks!

  • Matt Darby 28 posts 391 karma points c-trib
    Oct 26, 2017 @ 15:48
    Matt Darby
    0

    Hi Lucy,

    Do you have a sample of your code?

    Are you using Umbraco 7.6+? If so, MultiNodeTreePicker returns IEnumerable<IPublishedContent>

    You can use the Union method to create a union of the results like so:

    var mntp1 = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("mntp1");
    var mntp2 = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("mntp2");
    
    var union = mntp1.Union(mntp2);
    

    If you just want the string of names:

    var names = String.Join(", ", union.Select(m => m.Name));
    
  • Lucy Foster 10 posts 82 karma points
    Oct 26, 2017 @ 15:59
    Lucy Foster
    0

    Hi Matt,

    Yes I'm using Umbraco 7.6.6, so I've used IEnumerable<IPublishedContent>

    This is my foreach:

        @foreach(var test in SearchResults.Skip((page-1)*numPerPage).Take(numPerPage)){
            var thenode = Umbraco.Content(test.Id);
            var keepcats = thenode.GetPropertyValue<IEnumerable<IPublishedContent>>("keepAChild");
            var testlocs = thenode.GetPropertyValue<IEnumerable<IPublishedContent>>("locations");
            if(!String.IsNullOrWhiteSpace(@keepKey) && !String.IsNullOrWhiteSpace(@countryKey)){ // Has country & keep a child searched
                foreach(var keepitem in keepcats){
                    foreach(var locitem in testlocs){
                        if(keepitem.Name == @keepKey && locitem.Name == @countryKey){
                            <p>@thenode.Name | <b>@keepitem.Name</b> | <b>@locitem.Name</b></p>
                        }
                        if(keepitem.Name == @keepKey && locitem.Name != @countryKey){
    
                            <p>@thenode.Name | <b>@keepitem.Name</b> | @locitem.Name</p>
                        }
                        if(locitem.Name == @countryKey && keepitem.Name != @keepKey){
                            <p>@thenode.Name | @keepitem.Name | <b>@locitem.Name</b></p>
                        }
                    }
                }
            }
        }
    

    I've just tried using Union but I get this error:

    CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
    
Please Sign in or register to post replies

Write your reply to:

Draft