Copied to clipboard

Flag this post as spam?

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


  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    Jan 13, 2020 @ 12:06
    Jesper Skjønnemand
    0

    Is my item on my list?

    Hello I am trying the following:

    • Create a list of my schools (document type)
    • For each school on my list I want to
      • Create a list of available courses (multinode tree picker)
      • Check if that list contains the current page
      • If it does: Write Url and Name of the school page

    So far my template looks like this:

      @{
        var currentCourse = Model.Content.Id;
        var mySchools = Model.Content.Site().Descendants("Skole").ToArray();
            foreach(var item in mySchools) 
            {
            var schoolCourses = item.GetPropertyValue<IEnumerable<IPublishedContent>>("selectedCourses").ToArray();
            if (schoolCourses.Contains("CurrentCourse") == true)
                {
                <a href="@item.Url">@item.Name</a>
                }
            }
        }
    

    Umbraco does not like this line:

    if (schoolCourses.Contains("CurrentCourse") == true)
    

    I get this Compilation Error:

    CS1929: 'IPublishedContent[]' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains<string>(IQueryable<string>, string)' requires a receiver of type 'IQueryable<string>'
    

    What do I need to change? Best

  • Jonathan Distenfeld 105 posts 618 karma points
    Jan 15, 2020 @ 06:58
    Jonathan Distenfeld
    0

    Hi Jesper,

    try referencing namespace "System.Linq" on top of your template.

    @using System.Linq;

    ~ Jonathan

  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    Jan 22, 2020 @ 13:00
    Jesper Skjønnemand
    0

    Hi Jonathan

    I added @using System.Linq; in the top of my template without much success.

    Could it be my syntax with == true that is wrong?

  • Jonathan Distenfeld 105 posts 618 karma points
    Jan 23, 2020 @ 06:43
    Jonathan Distenfeld
    100

    Hi Jesper,

    the part with == true is not wrong, but you shouldn't need it anyway. Contains will return a bool you can check on.

    The problem is that you are trying to check if a string is part of an IPublishedContent-Array. This won't work, you will have to check for IPublishedContent.

    Try the following:

    @using System.Linq;
    @{
        var mySchools = Model.Content.Site().Descendants("Skole");
        foreach (var item in mySchools)
        {
            var schoolCourses = item.GetPropertyValue<IEnumerable<IPublishedContent>>("selectedCourses");
            if (schoolCourses.Contains(Model.Content))
            {
                <a href="@item.Url">@item.Name</a>
            }
        }
    }
    

    Hope this helps.

    ~ Jonathan

  • Jesper Skjønnemand 66 posts 441 karma points c-trib
    Jan 23, 2020 @ 09:06
    Jesper Skjønnemand
    1

    Hi Jonathan

    This works 100 % as I hoped it would.

    As you have probably guessed I am at the early stages of learning Umbraco templating, so this was a very usefull lesson.

    If this forum had a "send a nice cold IPA" function, I would definitely use it now.

    All the best

  • 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