How do i convert arraylist to List<Dictionary<string, string>>

Posted on 2009-12-24
Last Modified: 2012-06-21
The following is the code from my service.  I need to convert arraylist into
Dictionary<string, string>. The arraylist is in key/value.

E.g. Sample arraylist as follows:

array list (0): "UserID"
array list (1): "Tom"
array list (2): "ActivityName"
array list (3): "Login"

*********  Service code *********************
  public class Service1 : System.Web.Services.WebService

        protected List<Dictionary<string, string>> list;
        protected String activityname;
        public void WriteToBam(string ActivityName, object[] arrObj)
             ArrayList arrDB = new ArrayList(ar rObj);
            // Load the value from arraylist to Dictionary (key/value)

             BAMWriter bamWriter = new BAMWriter();
            bamWriter.writeToBamDatabase(list, ActivityName);

Thanks in advance
Question by:Lalu01
    LVL 30

    Expert Comment

    by:Reza Rad
    do you have multiple arraylists or you have one arraylist and on each item of this arraylist you have another arraylist.
    could you attach the code of creating your arraylist here
    LVL 8

    Accepted Solution


    // Will this work?
    for (int i=0; i<arrObj.Count; i+=2)
      list.Add(Convert.ToString(arrObj[i]), Convert.ToString(arrObj[i+1]))

    Open in new window

    LVL 11

    Expert Comment

    not sure what you are trying to achieve but your code seems to indicate you have an array of object that you use to create an arraylist to create a dictionnary.
    one thing you can do is use the function ToDictionnary of your object array to get your dictionnary right away.
    here is an example:
    Dictionary<string,string> myDict =arrObj.ToDictionary(key => key.ToString(), value => value.ToString());

    you ll probably have to change the lambda expressions for key and value to match what your data structure actually holds.

    Author Closing Comment


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now