Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Jan 15, 2015 @ 18:03
    Claushingebjerg
    0

    Foreach with multiple sources

    How do i join two sources in a single foreach statement?

    So instead of:

    @foreach(var nestedFs in fieldset.GetValue<ArchetypeModel>("leftproducts")){
    <p>stuff</p>
    }
    
    @foreach(var nestedFs in fieldset.GetValue<ArchetypeModel>("rightproducts")){ 
    <p>stuff</p>
    }


    I would have something like

    @foreach(var nestedFs in fieldset.GetValue<ArchetypeModel>("leftproducts") && fieldset.GetValue<ArchetypeModel>("rightproducts")){
    <p>stuff</p>
    }

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 20:08
    Jan Skovgaard
    0

    Hi Claus

    I'm not 100% certain but perhaps you can find some inspiration in the answers from Dennis in this post? http://our.umbraco.org/forum/developers/razor/59560-How-to-merge-two-NodebyID-into-one-variable?p=0

    /Jan

  • Proxicode 128 posts 324 karma points
    Jan 15, 2015 @ 20:27
    Proxicode
    100

    Yes Jan - this is exactly what I was thinking about as well. I am not familiar with the ArcheTypeModel, but since it appears that the return type is enumerable, something like this should work.

    @{
        var left = fieldset.GetValue<ArchetypeModel>("leftproducts");
        var right = fieldset.GetValue<ArchetypeModel>("rightproducts");
        var allProducts = left.Concat(right).ToList();
    
        foreach(var nestedFs in allProducts){
            <p>stuff</p>
        }
    }
    

    You can use Union or Concat, the former removes duplicates, the later doesn't

    -Roger

  • Claushingebjerg 939 posts 2574 karma points
    Jan 16, 2015 @ 09:23
    Claushingebjerg
    0

    Perfect :)

Please Sign in or register to post replies

Write your reply to:

Draft