Copied to clipboard

Flag this post as spam?

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


  • Liam Dilley 153 posts 379 karma points
    May 12, 2020 @ 06:02
    Liam Dilley
    0

    Getting items where multi tree node property contains ID = to current Model

    Hi All, I have been trying various solutions for this but not able to find one that works properly.

    I either get:

    • Object not instance of an object
    • Value cannot be null
    • And other IEnumerable errors.

    I have tried the following:

    var items = Model.Root().Children.Where(x => x.Name == "News").FirstOrDefault().Children.Where(x => x.Value<IEnumerable<IPublishedContent>>("relatedProjects").Select(p => p.Id).Contains(Model.Id));
    
    var selectedNews = new List<int>();  var items = Model.Root().Children.Where(x => x.Name == "News").FirstOrDefault().Children
        .Where(x => x.Value<IEnumerable<IPublishedContent>>("relatedProjects")
        .Select(y => y.Id).Intersect(selectedNews).Count() == selectedNews.Count)
        .Select(x => x.Id);
    

    And a number of other variations of SELECT and WHERE but I have not been able to figure it out.

    I want to get all news that are tagged for the current project and then loop through them. It could be 1 item or 10.

    Can anyone help please?

  • Liam Dilley 153 posts 379 karma points
    May 13, 2020 @ 03:39
    Liam Dilley
    0

    Another issue I have found is that if there is only one node selected there seems to be errors produced. Even though you can set a limit of one or there is the possibility of 1 if you fetch as per the documentation it produces an error.

  • Liam Dilley 153 posts 379 karma points
    May 13, 2020 @ 07:56
    Liam Dilley
    0

    For anyone...

    They key here is to check that the IPublishedContent is not null. This will be the case if there are no items in one of the items.

    So before our Where and select as in my code in the OP you add..

    .Where(x => x.Value<IEnumerable<IPublishedContent>>("relatedProjects") != null)
    
Please Sign in or register to post replies

Write your reply to:

Draft