Copied to clipboard

Flag this post as spam?

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


  • Hayden 32 posts 170 karma points
    Oct 04, 2017 @ 02:43
    Hayden
    0

    .Where statement to pull in records where an Content ID is apart of a multi-node treepicker list

    Hi all,

    I'm trying to pull in a list of case studies where a content ID (this is a user page, so sort of a user ID) existings within a multi-node treepicker field (each case study page has a list of users involved in each project).

    So....

    Case Study Node: projectMembers field (multi-node treepicker field which contains IDs of content pages from teams page).

    FYI: the "1107" is just hardcoded it's an ID that was listed on the page. The ID. I'd also need to support multiple IDs, so contains a bunch of ID's...

    It's basically a multi search features, I have a list of users & a list of categories. if a person has selected two users, it will search for products with both of their IDs involved :).

    foreach(var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue("projectMembers").Contains("1107")))
    {
        //echo out ID in here, I can do this part :)
    }
    

    This is code I've found being used around the place but doesn't seem to work for my case...

    CS1593: Delegate 'System.Func

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 04, 2017 @ 17:01
    Alex Skrypnyk
    0

    Hi Hayden

    Try this code:

    foreach(var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue<string>("projectMembers").Contains("1107")).Select(x => x.Id))
    {
        @item
    }
    

    Thanks,

    Alex

  • Hayden 32 posts 170 karma points
    Oct 04, 2017 @ 20:30
    Hayden
    0

    Hi Alex,

    I'm getting strange results here, using your code & the ID of 1107 (which I can confirm is the node ID of a team page user).

    This returns 0 results.

    However when I change the ID in the Contains statement to just Contains("1") it returns every case study (even ones with no value in the multi node tree picker.

    Can you think of any reason this would be the case?

    FYI: I'm using Umbraco 7.6.7 if that makes a difference -> I know the IDs have changed between 7.5 -> 7.6 so could be something there. I thought it may be searching for guids rather than node IDs but doesn't seem to be the case.

    Cheers, Hayden

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 04, 2017 @ 20:39
    Alex Skrypnyk
    101

    Hayden,

    Is "Property Values Converter" enabled in your project?

    If yes, the code should be:

    foreach(var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("projectMembers").Select(y => y.Id).Contains(1107)).Select(x => x.Id))
    {
        @item
    }
    
  • Hayden 32 posts 170 karma points
    Oct 04, 2017 @ 20:56
    Hayden
    0

    Hi Alex,

    I can confirm that EnablePropertyValueConverters is set to true

    When I run the code above I get the error I had originally...

    Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue>("projectMembers").Select(y => y.Id).Contains(1107)).Select(x => x.Id))
    

    I'm assume the > after GetPropertyValue was a mistake :)?

    CS1593: Delegate 'System.Func

  • Hayden 32 posts 170 karma points
    Oct 04, 2017 @ 21:00
    Hayden
    0

    Hi Alex

    Ignore my above post, I have miscopied your snippet :).

    It is now working!

    Thank you so much, have been banging my head on this for awhile!!

    Cheers, Hayden

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 04, 2017 @ 21:01
    Alex Skrypnyk
    0

    Hayden, really glad to help you! Ask questions if you have and have a great evening!

  • Hayden 32 posts 170 karma points
    Oct 04, 2017 @ 21:31
    Hayden
    0

    Hi Alex,

    I was wondering if there's an easy way to convert this to accept multiple IDs.

    So say a user has selected users 1107, 1108 and want to find any case studies that these two IDs are involved in.

    Is there a function similar to contain that accepts an array maybe?

    This is what I have at the moment...

    URL: /case-studies/?search=1&member=1107&member=1108

    var selected_members = new List<int>();
    
    if(!Request.Params["member"].IsEmpty())
    {
        foreach(var member in Request.Params["member"].Split(','))
        {
            selected_members.Add(Convert.ToInt32(member));
        }
    }
    
    @foreach(var mem in selected_members)
    {
        <p>Mem: @mem</p>
    }
    
    // This returns...
    
    Mem: 1107
    
    Mem: 1108
    

    So is there a function like ArrayContains(selected_members)?

    foreach(var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("projectMembers").Select(y => y.Id).Array.IndexOf(selected_members)).Select(x => x.Id))
    

    I have found IndexOf, but unsure of how to go about using it :).

    Appreciate it, others will probably be curious on how to do this as well.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 05, 2017 @ 08:39
    Alex Skrypnyk
    1

    Hi Hayden

    It looks like you need this code:

    var selected_members = new List<int>();
    foreach (var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy")
        .Where(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("projectMembers")
        .Select(y => y.Id).Intersect(selected_members).Count() == selected_members.Count)
        .Select(x => x.Id))
    {
    }
    
  • Hayden 32 posts 170 karma points
    Oct 05, 2017 @ 20:28
    Hayden
    1

    Legend! no wonder you have an MVP tag :).

    That works perfectly, hopefully will help other community members as well.

    Cheers, Hayden

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 05, 2017 @ 20:30
    Alex Skrypnyk
    0

    Thanks, Hayden for your words )

  • Hayden 32 posts 170 karma points
    Oct 05, 2017 @ 21:01
    Hayden
    0

    Hi Alex,

    Not sure what method I'm going to use yet, but would it be tricky to find ALL projects that have at least one of the "selected_members" within?

    So selected members are... 1107, 1108 & 1112
    
    Projects as followed...
    
    Project #1: contains 1107, 1113
    Project #2: contains 1108, 1112
    Project #3: contains 1106, 1107
    
    So, for our query above of searching for projects with 1107, 1108 & 1112 it would return all projects because atleast one of the members is in each
    

    At the moment it requires all of the selected members to be within the project for it to be returned as a result.

    Also, do you have any recommendations for guides on learning how to best make use of .where() / Linq, I'm from a PHP background so more use to SQL but I'm slowly understanding it!

    Cheers, Hayden

  • Hayden 32 posts 170 karma points
    Oct 05, 2017 @ 21:10
    Hayden
    1

    Hi Alex,

    I have solved this one myself, it was actually a simple change. Just had to check that the returns members was equal or more than 1, rather than seeing if it matched the amount of selected users.

    For anybody who's interested in this method :)

    @{
                foreach (var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("projectMembers").Select(y => y.Id).Intersect(selected_members).Count() >= 1).Select(x => x.Id))    
                {
                    <div class="col-md-6">
                        <p>Item: @item</p>
                    </div>
                }
            }
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 05, 2017 @ 21:20
    Alex Skrypnyk
    1

    Hi Hayden

    Thanks for sharing with the community!

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft