Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    May 18, 2016 @ 12:12
    Paul Griffiths
    0

    LINQ for looping through pages and checking a property value

    Hi All,

    Can someone please point where i am going wrong with this linq?

    var photoStaffList = siteRoot.Descendants("staffProfile").Where(x => x.GetPropertyValue<string>("role").Contains("photo")).ToList();
    

    I want to find all the pages that are of type 'staffProfile' and then loop through each of those pages checking whether a value set in the role poperty (dataType multi dropdown picker) contains the word 'photo'.

    I keep getting 0 results returned.

    Thanks Paul

  • Ben Abbott 19 posts 124 karma points
    May 18, 2016 @ 13:15
    Ben Abbott
    0

    Try this....

    var siteRoot = Model.Content.AncestorOrSelf(1);
    
    var photoStaffList = (from c in siteRoot.Descendants()
                          where c.ContentType.Alias.Equals("staffProfile", StringComparison.CurrentCultureIgnoreCase)
                             && c.GetPropertyValue<String>("role").Contains("photo", StringComparison.CurrentCultureIgnoreCase)
                          select c).ToList();
    
  • Paul Griffiths 370 posts 1021 karma points
    May 18, 2016 @ 14:33
    Paul Griffiths
    0

    Hi Ben,

    Thans for the response, im getting the following compile error using your code

    enter image description here

    Thanks Paul

  • Steve Morgan 1350 posts 4460 karma points c-trib
    May 18, 2016 @ 15:17
    Steve Morgan
    0

    Going back to your original code - I think you've found a bug in how Umbraco converts the Multiple Dropdown to a string in Linq. I've just tried this and I get a similar issue.

    The only workaround I can see is to get all the descendants and then test that property in a foreach loop.

    You should report this as a bug I think.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    May 18, 2016 @ 15:35
    Nicholas Westby
    0

    My guess is that GetPropertyValue<string>("role") is returning a CSV of prevalues (e.g., "123,456,789") rather than a CSV of strings (e.g., "role1,role2,role3"). Here are some things you can do to investigate.

    @{
        var nodes = siteRoot.Descendants("staffProfile").ToArray();
        var values = string.Join(" * ", nodes.Select(x => x.GetPropertyValue<string>("role")).ToArray());
    }
    <p>There are @(nodes.Length) nodes and the values are @(values).</p>
    

    I'm not in an IDE right now, so I'm not sure if all that syntax is correct, but you get the idea.

  • Paul Griffiths 370 posts 1021 karma points
    May 18, 2016 @ 15:38
    Paul Griffiths
    0

    Hi All,

    Thanks for the responses with this one. @Nicholas i will give your solutions a go and see what i get :)

    Using @Ben's posts, i can achieve what i want with the following code

    //gets all the photography staff members
    var photoStaffList = (from c in siteRoot.Descendants()
                          where c.ContentType.Alias.Equals("staffProfile", StringComparison.CurrentCultureIgnoreCase)
                             && c.GetPropertyValue<string>("role").ToLower().Contains("photo")
                          select c).ToList();
    

    Be good to have a play around though.

    Many thanks Paul

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies