Copied to clipboard

Flag this post as spam?

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


  • Kasper Dyrvig 246 posts 379 karma points
    Aug 22, 2012 @ 13:23
    Kasper Dyrvig
    0

    Loop only if this has value - not working as expected

    Hallo...

    I'm working with some complex coding. But I'm stuck at a very simple if statement:

        string hotelNodeString "";
        foreach(var season in Model.Children.Where("Visible"))
        {
          hotelNodeString hotelNodeString season.seasonHotels;
          if(!season.IsLast())
          {
           hotelNodeString hotelNodeString ",";
          }
        }
        string[accommodationHotels hotelNodeString.Split(',');
         
        string shipNodeString "";
        foreach(var season in Model.Children.Where("Visible"))
        {
         if(season.HasValue("seasonShips"))
         {
          shipNodeString shipNodeString season.seasonShips;
          if(!season.IsLast())
          {
           shipNodeString shipNodeString ",";
          }
         }
        }
        string[accommodationShips shipNodeString.Split(',');
        
        List<stringaccommodations new List<string>();
        foreach(string st in accommodationShips)
        {
         if (!accommodations.Contains(st))
         {
          accommodations.Add(st);
         }
        }
        foreach(string st in accommodationHotels)
        {
         if (!accommodations.Contains(st))
         {
          accommodations.Add(st);
         }
        }

    For some reason the first foreach loop output a comma.

    I need to check if the seasonHotels has a value. If not the loop shoudn't add anything.

    I have tried to do this:

        foreach(var season in Model.Children.Where("Visible"))
        {
         if(season.HasValue("seasonHotel"))
         {
          hotelNodeString hotelNodeString season.seasonHotels;
          if(!season.IsLast())
          {
           hotelNodeString hotelNodeString ",";
          }
         }
        }

    But it is the same result. All ideas is appreciated.

  • Kasper Dyrvig 246 posts 379 karma points
    Aug 22, 2012 @ 14:27
    Kasper Dyrvig
    0

    Oh well... I have fixed it for now:

        string accommodationNodeString "";
        foreach(var season in Model.Children.Where("Visible"))
        {
         if(season.seasonHotels != "")
         {
          accommodationNodeString accommodationNodeString season.seasonHotels;
         }
         if(season.seasonShips != "")
         {
          accommodationNodeString accommodationNodeString season.seasonShips;
         }
         if(!season.IsLast())
         {
          accommodationNodeString accommodationNodeString ",";
         }
        }
        string[accommodationIDs accommodationNodeString.Split(',');
        List<stringaccommodations new List<string>();
        foreach(string st in accommodationIDs)
        {
         if (!accommodations.Contains(st))
         {
          accommodations.Add(st);
         }
        }
Please Sign in or register to post replies

Write your reply to:

Draft