Copied to clipboard

Flag this post as spam?

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


  • James Feeney 26 posts 166 karma points
    Sep 11, 2013 @ 16:42
    James Feeney
    0

    Collect items from variable repository name.

    I've tried so many different ways of getting items that have a repository name of a variable via a querystring.

    Here is my content tree:

    Content
      - > Homepage
            - > Recipe Repository (named rep1)
    - > Recipe Item
    - > Recipe Item
    - > Recipe Item
    - > Recipe Item
    - > Recipe Repository (named rep2)
     - > Recipe Item
     - > Recipe Item
    - > Recipe Repository (named rep3)
     - > Recipe Item
     - > Recipe Item
    - > Recipe Repository (named rep4)
     - > Recipe Item
     - > Recipe Item

    string collect = querystring that will match one of five repositories (equalling either rep1, rep2, rep3 or rep4)


    To get items matching querystring repository, I've tried:

    Model.AncestorOrSelf().Descendants.Where("NodeTypeAlias == "+@collect).Children;

    Model.AncestorsOrSelf().RecipeRepositories(@collect).RecipeItems;

    What is the correct way of setting this out? I'm going crazy over this and I know it will be something incredibly obvious. Thanks in advance.

     

     

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 11, 2013 @ 18:20
    Ali Sheikh Taheri
    0

    try this

    Model.AncestorOrSelf(1).DescendantsOrSelf().Items.Where(x=>x.Name == collect)
    

    assuming that all the "Recipe Repository" have the same Document Type Alias.

    if rep1/rep2 are the name of the node than use above but if they are different document type. then use below:

    Model.AncestorOrSelf(1).DescendantsOrSelf().Items.Where(x=>x.NodeTypeAlias== collect)
    
  • James Feeney 26 posts 166 karma points
    Sep 12, 2013 @ 10:22
    James Feeney
    0

    Hi Ali,

    Thanks for that. The first one you gave looks like the right one. All repositories have the same Document Type I tried putting it in and I got this error:

    "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type".

    I think my problem is using that code in a foreach:

     

    foreach (var item in Model.AncestorOrSelf(1).DescendantsOrSelf().Items.Where(x=>x.Name == collect)) {
    ** do something for each recipe's properties: (item.Name, item.Image, item.description) in the chosen repository ** }

    Do I need to collect the recipes into an array and then use that in the foreach for this to work?

     

  • James Feeney 26 posts 166 karma points
    Sep 12, 2013 @ 11:20
    James Feeney
    0

    Tried this from another recent post, but still no luck:

    var recipeitems = Model.Content.Children.Where("DocumentTypeAlias == \"@collect\"");
    foreach (var item in recipeitems) { }
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 12, 2013 @ 11:22
    Ali Sheikh Taheri
    0

    you dont need to do that actually. it should just work because the result of

    Model.AncestorOrSelf(1).DescendantsOrSelf().Items.Where(x=>x.Name == collect)
    

    would be IEnumerable

    I would use CurrentModel to get proper intellisense in visual studio

     var rep = CurrentModel.AncestorOrSelf(1).DescendantsOrSelf().Items.Where(x => x.Name == collect).ToList();
    
    foreach (var node in rep)
    {
        @node.GetPropertyValue("myProperty")
    }
    
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 12, 2013 @ 11:41
    Ali Sheikh Taheri
    100

    update :

    var rep = CurrentModel.AncestorOrSelf(1).DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect);
    
    if (rep != null)
    {
        foreach (var node in rep.ChildrenAsList)
        {
            @node.Name
            @node.GetPropertyValue("myProperty")
        }
    }
    
  • James Feeney 26 posts 166 karma points
    Sep 12, 2013 @ 14:31
    James Feeney
    0

    Brilliant! Thanks Ali, that worked a charm.
    I had to remove AncestorsOrSelf(1) because I moved the repositories into a parent repository, but works great!

    Thanks 

Please Sign in or register to post replies

Write your reply to:

Draft