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 126 posts 298 karma points
    18 days ago
    Robin Hansen
    0

    Compare two Multinode Treepickers

    I need to compare the value (key) of two Multinode Treepickers, and get a list of the nodes where the values are equal.

    In my doctype I have a Multinode Treepicker with a list of members, and on my Member Profile I also have a Multinode Treepicker with members.

    How can I make a foreach loop showing only theese nodes withe the same members as on my member profile?

    My Member:

    var profileModel = Members.GetCurrentMember();
    var followings = profileModel.Value<IEnumerable<IPublishedContent>>("profileFollowings");
    var followingsKeys = followings.Select(x => x.Key.ToString()).ToList();
    

    My Doctype:

    var items = Model.Children.Where(x => x.Value<IEnumerable<IPublishedContent>>("categoryItemRecommenders").Select(y => y.Key.ToString()).ToList().SequenceEqual(followingsKeys));
    

    It does'nt quite work - items returns 0 - what am I missing...?

  • Marc Goodson 1584 posts 10551 karma points MVP 6x c-trib
    17 days ago
    Marc Goodson
    0

    Hi Robin

    Hard to be sure without having the code in front of me, but are you basically trying to say, get me all the Category Item Recommenders who are ALSO in the current Logged in Member's profile followers?

    And if so, would this be the sort of thing you are after:

       var recommendersThatFollow = Model.Value<IEnumerable<IPublishedContent>>("categoryItemRecommenders").Where(f=>followingsKeys.Contains(f.Key.ToString()));
    

    or have I misunderstood the content tree you are working with?

    regards

    Marc

  • Robin Hansen 126 posts 298 karma points
    17 days ago
    Robin Hansen
    0

    Hi Marc, you have understood my problem correct. I have allready tried your approach, but it does'nt work because it is two lists I'm comparring, not checking if my list contains a string. I can provide the whole code sample if it is needed... \Robin :)

  • Marc Goodson 1584 posts 10551 karma points MVP 6x c-trib
    16 days ago
    Marc Goodson
    100

    Hi Robin

    I think I may have misunderstood, then seeing the tree structure would help...

    ... I'm now thinking you have:

    • ThisPage

      • CategoryItemPage
      • CategoryItemPage
      • CategoryItemPage
      • CategoryItemPage
      • CategoryItemPage

    And each 'CategoryItemPage' has the MemberPicker with alias 'categoryItemRecommenders'

    So IF you are on the 'ThisPage', ALL the category pages would be accessible via:

    var categoryItemPages = Model.Children();

    But you only want the catetoryItemPages where the picker on the page includes any one of the picked members on the Current Member...

    so I think it's really close to what you had before but something more like this:

    var items = Model.Children.Where(x => x.Value<IEnumerable<IPublishedContent>>("categoryItemRecommenders").Select(y => y.Key.ToString()).Intersect(followingKeys).Any());
    

    eg Get all the CategoryItemPage children where the picker's array of keys contains at least one match in the array of followingKeys.

    Intersect will give you a new Array of all the matching elements in the two Arrays, and calling .Any() means the comparison will stop, just as soon as it finds 'one'!

    well that's if' I've understood :-P

    regards

    Marc

  • Robin Hansen 126 posts 298 karma points
    1 week ago
    Robin Hansen
    0

    Hi Marc, Your understanding was correct, and your code sample (with a little twist) helped me a lot...

    This is how I ended up using it:

        var followingsKeys = followings.Select(x => x.Key.ToString()).ToList();
        HashSet<string> equalkeys = new HashSet<string>(followingsKeys);
        var items = Model.Children.Where(x => x.Value<IEnumerable<IPublishedContent>>("categoryItemRecommenders").Select(y => y.Key.ToString()).Intersect(equalkeys).Any());
    

    This sample gave me the desired result... H5y!

Please Sign in or register to post replies

Write your reply to:

Draft