Copied to clipboard

Flag this post as spam?

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


  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 10:32
    Brian Olsen
    0

    Razor foreach

    I use umbraco 4.11.5

    I need a foreach loop where it compares two comma separated lists 

    the xml

              <faser><![CDATA[Skole start, Mellem trin]]></faser>

    and the second comma-separated lists are a form request 

    RQfaser = Request["faser"];
    RQfaser = "BH, Skole start, Mellem trin"

    if one or more of the elements are the same, then print, but I would only have one item per child

     

     

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    May 28, 2013 @ 10:38
    Dave Woestenborghs
    0

    You can split both your strings on the comma which will give you 2 array's.

    Then you can compare the 2 arrays. Here is a stack overflow post about comparing 2 arrays : http://stackoverflow.com/questions/10991452/how-do-i-get-the-differences-between-two-string-arrays-in-c

     

    Dave

  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 11:03
    Brian Olsen
    0

    Hi dawoe

    I'm new in Razor

    How do I convert them to array

  • kim Thomsen 59 posts 277 karma points
    May 28, 2013 @ 13:14
    kim Thomsen
    1

    You can do something like this:

    @foreach(string variable in RQfaser.Split(","), StringSplitOptions.RemoveEmptyEntries))
    {
    if(arrayToCompareTo.Contains(variable)){
    //ok
    }else{
    //not in array
    }
    }

      

  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 13:30
    Brian Olsen
    0

    Hi Kim,

     

    if arrayToCompareTo is a comma-separated how do I make it so????

  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 13:39
    Brian Olsen
    0

    Hi Kim,

    Yes it works :-)

    thank you

  • kim Thomsen 59 posts 277 karma points
    May 28, 2013 @ 13:40
    kim Thomsen
    0

    it doesen mater if arrayToCompareTo is a comma seperatede string .Contains will just look for the "variable" in the string.
    so if  
    variable ="Skole start"

    It wil return true if arrayToCompareTo contains thats string

    so you only have to split one of the strings

  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 14:01
    Brian Olsen
    0
    Hi Kim,
    Sorry it's not quite
    foreach(var item in @Model.Children.Where("Visible")) 
    {
    string Sfaser = @item.faser;
    foreach(string variable in @RQfaser.Split(',')){
    if(Sfaser.Contains(variable)){
    @item.faser

    }else{} 
    }
    }
    Xml @Model.Children.Where
    1 
    2
    results:

    BH, Udskoling
    BH, Udskoling
    Skole start, Mellem trin

    and the results should be

    BH, Udskoling
    Skole start, Mellem trin

    can you see where the problemt is??

  • kim Thomsen 59 posts 277 karma points
    May 28, 2013 @ 14:30
    kim Thomsen
    100

    This should help then :-)

    foreach(var item in@Model.Children.Where("Visible")) 
    {
    stringSfaser=@item.faser;    
    foreach(string variable in@RQfaser.Split(',')){
    if(Sfaser.Contains(variable)){
    @variable
    }
  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 15:00
    Brian Olsen
    0

    and it did

    thanks :-) 

    just one little thing more if there is only one thing in the list then it's not a comma-separated list and then it jumps over it how can I get it to print if it is true

  • Brian Olsen 143 posts 424 karma points
    May 28, 2013 @ 16:29
    Brian Olsen
    0

    Hi Kim

    forget it

    I reverse it and now it works as it should

    a thousand thanks

Please Sign in or register to post replies

Write your reply to:

Draft