How to loop through two arraylist?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Rodrigues
    New Member
    • Dec 2010
    • 16

    How to loop through two arraylist?

    this is my data,

    cinema1 4pm
    cinema1 5pm
    cinema1 7pm
    cinema2 2pm
    cinema2 4.45pm
    cinema3 10.45am

    These are 2 different coloumns which I get from a webservice which can be arranged into an arraylist or array.. Now what I need is..the data to be displayed as follows..

    cinema1
    4pm 5pm 7pm
    -----------------
    cinema2
    2pm 4.45pm
    -----------------
    cinema3
    10.45am
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't know if separate arrays are appropriate in this case... you need some way to directly associate the times to the cinema. You may want to look at a key value pair, or perhaps having new data type that you can put in an array.

    Perhaps that data type can be something like...

    Code:
    public class ScheduleItem
    {
      public string CinemaName { get; set; }
      public List<DateTime> ShowTimes { get; set; }
      ...
    }
    Then you can have a list of ScheduleItems and output accordingly?

    If you absolutely must use two independent arrays, you will need something else to define how your data maps together...

    Comment

    • Colin Rodrigues
      New Member
      • Dec 2010
      • 16

      #3
      I could even do it using Dictionary..can u help me in that

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        You can, yes... but your value will still have to be another object, which are a list of times. Here is the MSDN page for a Dictionary, have a read and give it a try. If you run into problems, post what you've got and we'll take a look.

        Comment

        • NitinSawant
          Contributor
          • Oct 2007
          • 271

          #5
          use Linq group by clause on both the arraylists

          Comment

          • Colin Rodrigues
            New Member
            • Dec 2010
            • 16

            #6
            Thanks guys for your help...however. .i have used a dictionary and two new arrays to store the data... my final output is like this..
            arraylist1----arraylist2
            Cinema1 ----4pm 5pm 7pm
            Cinema2---- 2pm 4.45pm
            Cinema3----10.45am

            Now my next task is to print alternate values from these arraylist into a html table..

            it should look like this
            =============== ===============
            Cinema1
            =============== ===============
            4pm 5pm 7pm
            =============== ===============
            Cinema2
            =============== ===============
            2pm 4.45pm
            =============== ===============

            Comment

            Working...