Copied to clipboard

Flag this post as spam?

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


  • Ault Nathanielsz 87 posts 407 karma points c-trib
    Sep 09, 2022 @ 16:05
    Ault Nathanielsz
    0

    LINQ, MNTP and the current node

    In Umbraco 10, I'm trying to get a list of projects that have the current node (an agency) picked in an mntp.

    My inclination is this:

    @foreach (WViewProject project in Umbraco.Content(Guid.Parse("258f6eb4-a07c-4cb1-9b9a-20b021ea296f"))!.Children()!.Where(x => x.Value<string>("WProjAgency") != "" && x.Value<IEnumerable<IPublishedContent>>("WProjAgency")!.ContainsAny<IPublishedContent>(Model)).OrderBy(x => x.Value<string>("WWpPropTitleShort")))

    I know that the list of projects is good, as I can remove the where statement and I get the expected list of all the projects. The only hurdle is that last bit where I attempt to see if the mntp (WProjAgency) contains @Model.

    For clarity:

    && x.Value<IEnumerable<IPublishedContent>>("WProjAgency")!.ContainsAny<IPublishedContent>(Model)

    I think I'm fairly close, just missing a small element.

    That said, if there is a more eloquent way to do this, please let me know.

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    Sep 09, 2022 @ 16:14
    Ault Nathanielsz
    100

    Solved... Two errors in the code above.

    ContainsAny() should be Contains()

    e.g. x.Value<IEnumerable<IPublishedContent>>("WProjAgency")!.Contains<IPublishedContent>(Model)

    Also, the Null Check should be:

    x => x.Value<IEnumerable<IPublishedContent>>("WProjAgency") is not null

    The full statement that works is:

    x => x.Value<IEnumerable<IPublishedContent>>("WProjAgency") is not null && x.Value<IEnumerable<IPublishedContent>>("WProjAgency")!.Contains<IPublishedContent>(Model)

  • 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