Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 886 posts 2415 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 11258 posts 23500 karma points MVP 7x 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 119 posts 305 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 886 posts 2415 karma points
    Jan 16, 2015 @ 09:23
    Claushingebjerg
    0

    Perfect :)

  • 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