Copied to clipboard

Flag this post as spam?

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


  • Mathias Valentin 60 posts 208 karma points
    Apr 12, 2016 @ 11:32
    Mathias Valentin
    0

    Filtering on custom property with .Where()

    Hi,

    I have a collection of project data (see below) in a root content folder shared by multiply websites located on root level. A website contains projects, and these projects is linked to the corresponding shared project data with an projectId string (property on both project and project data).

    -Website (da)
    --Projects
    ---project 1
    ---project 2
    
    -Website (en)
    --Projects
    ---project 1
    ---project 2
    
    -Projects data
    --project 1
    --project 2
    

    I'm now trying to find the best way of getting the linked project data on each project in my partial view. This is what I got so far (not working):

    var contentRoot = Umbraco.TypedContentAtRoot();
    
    var projectsFolder = contentRoot.First().Sibling("projectsFolder");
    var project = projectsFolder.Descendants("projectData").Where("projectId == CurrentPage.projectId");
    

    The above method isn't giving any results - the .Where() part is not working :(

    How would you guys get the correct projectData node? Am I on the right path? Is this real life? etc.

    /Mathias

  • Gordon Saxby 1444 posts 1855 karma points
    Apr 12, 2016 @ 13:03
    Gordon Saxby
    1

    I haven't looked at what you are trying to do in detail - but I think your "where" should be more like

    var project = projectsFolder.Descendants("projectData").Where(p => p.projectId == CurrentPage.projectId);
    
  • Mathias Valentin 60 posts 208 karma points
    Apr 12, 2016 @ 19:53
    Mathias Valentin
    0

    Thanks a million, Gordon!

    Tried your suggestion and ended up with an error?! But eventually got it working with:

    var project = projectsFolder.Descendants("projectData").Where(p => p.GetPropertyValue<string>("projectId") == CurrentPage.projectId);
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Apr 12, 2016 @ 13:35
    Steve Morgan
    1

    Looks like there is also a problem at line two - you're not getting the ProjectsFolder - a sibling is at the same level not below.

    Try:

    var contentRoot = Umbraco.TypedContentAtRoot();
    
    var projectsFolder = contentRoot.Children("projectsFolder").First();
    var project = projectsFolder.Descendants("projectData").Where(x => x.Id == projectId);
    
  • Mathias Valentin 60 posts 208 karma points
    Apr 12, 2016 @ 19:46
    Mathias Valentin
    0

    Hi Steve,

    Thanks for your answer! I actually tried that already, but it does not seem to work as expected (see error below). However the contentroot.First().Siblings() does the job, but it feels a bit like a hack :)

    CS1928: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Children' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.Children(Umbraco.Core.Models.IPublishedContent, System.Func<Umbraco.Core.Models.IPublishedContent,bool>)' has some invalid arguments
    

    Your answer got me looking at the docs again and I found that TypedContentAtXPath can be used instead, it seems to be a cleaner/better solution:

    var projectsFolder = Umbraco.TypedContentAtXPath("//projectsFolder").First();
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Apr 13, 2016 @ 07:35
    Steve Morgan
    0

    Hi,

    Sorry my code should have said

    var contentRoot = Umbraco.TypedContentAtRoot();
    
    var projectsFolder = contentRoot.Children().Where(x => x.DocumentTypeAlias == "projectsFolder").First();
    var project = projectsFolder.Descendants("projectData").Where(x => x.Id == projectId);
    

    You can use XPaths but it's a bit of a throw back from the old XSLT Umbraco days. That said the cache is still XML based so it should be speedy enough.

    Steve

  • Mathias Valentin 60 posts 208 karma points
    Apr 13, 2016 @ 07:41
    Mathias Valentin
    0

    Oh - didn't know that! I'm trying your code but seems to get the same error. It's complaining about the contentRoot.Children ("does not contain a definition for 'Children'")?

    CS1928: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Children' and the best extension method overload 'Umbraco.Core.Models.ContentExtensions.Children(Umbraco.Core.Models.IContent)' has some invalid arguments
    

    I can't even do a foreach on @contentRoot.Children()?

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Apr 13, 2016 @ 07:53
    Kevin Jump
    1

    Hi

    if you have multiple root nodes TypedContentAtRoot will return a collection not a single item, so that might be why it doesn't have .Children() directly.

    Model.Content.AncestorsOrSelf(1) 
    

    will return the root level for a given page (it says go to level 1 for this node)

  • Mathias Valentin 60 posts 208 karma points
    Apr 13, 2016 @ 07:58
    Mathias Valentin
    0

    Thanks Kevin! That makes perfectly good sense. How would you then get a root level sibling from within a given page? Would this be the best approach?

    Model.Content.AncestorsOrSelf(1).Sibling("projectsFolder")
    
  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Apr 13, 2016 @ 08:03
    Kevin Jump
    1

    Hi

    Personally for that i would use TypedContentAtRoot, and then get the sibling from the collection, although I am not sure why, the above probably works just as well :)

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Apr 13, 2016 @ 08:17
    Steve Morgan
    0

    Doh - good spot Kevin. Right, never post code without trying it first :)

Please Sign in or register to post replies

Write your reply to:

Draft