Copied to clipboard

Flag this post as spam?

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


  • Levente Kosa 136 posts 352 karma points
    Jun 09, 2016 @ 17:15
    Levente Kosa
    0

    Razor foreach checking value instantly

    Hi,

    Is there a chance to check a value within a foreach at the same time? I would like to filter by value and show randomly only one.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jun 09, 2016 @ 18:24
    Jan Skovgaard
    0

    Hi Levente

    What do you have in mind?

    Something like

    @foreach (item in Model.News){
    
        if(item.Headline.HasValue){
              //Do something
         }
    }
    

    Or are you thinking more like

    @foreach(item in Model.News.Where("Condition")){
          //Do something
    }
    

    Perhaps you can benefit from the Razor cheetsheet here https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets/ - It's made for Umbraco 6 but should be relevant for v7 as well - Be aware that the one I linked to is for strongly typed razor - If you're using dynamics then you should have a look at this cheatsheet instead https://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Look for the "Filtering and ordering extensions".

    Hope this helps.

    /Jan

  • Levente Kosa 136 posts 352 karma points
    Jun 09, 2016 @ 19:56
    Levente Kosa
    0

    Definitely the second what I need. Thank you for the links, but I can't deal with it, this is what I have at the moment:

    var projectImagesList = CurrentPage.projectImages.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    var projectImagesCollection = Umbraco.Media(projectImagesList);
    
    foreach (var projectImage in projectImagesCollection.Where("Visible").Random(1))
    {
        if (imageType == "medium-landscape")
        {
            do something
        }
    }
    

    And I need to check imageType == "medium-landscape" outside.

  • 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