Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Sir,

Good day to all!
I would like to ask on how could I merge 4 List Collections in 1 List and all records
must be display per row from 4 list collections.

my code did only the .AddRange but it only merges all records as a whole.

here's my sample code scenario:

private static void Dump(Dictionary<string,>>> main_area)
        {
            List<string> lister1 = new List<string>();
            List<string> lister2 = new List<string>();
            List<string> lister3 = new List<string>();
            List<string> lister4 = new List<string>();

            int ToTal = 0;
            foreach (string Tag in main_area.Keys)
            {
                foreach (string MainArea in main_area[Tag].Keys)
                {
                    foreach (string Cluster in main_area[Tag][MainArea].Keys)
                    {
                        Console.WriteLine("{0} {1} {2} count: {3}", Tag, Cluster, MainArea, main_area[Tag][MainArea][Cluster]);
                        ToTal += main_area[Tag][MainArea][Cluster];

                        // reformatting
                        if (Tag.Trim() == "3G")
                        {
                            string out3G = Tag + " " + MainArea + " " + Cluster + " " + main_area[Tag][MainArea][Cluster];
                            lister1.Add(out3G);
                        }
                        if (Tag.Trim() == "GBB")
                        {
                            string outGBB = Tag + " " + MainArea + " " + Cluster + " " + main_area[Tag][MainArea][Cluster];
                            lister2.Add(outGBB);
                        }
                        if (Tag.Trim() == "DL")
                        {
                            string outDL = Tag + " " + MainArea + " " + Cluster + " " + main_area[Tag][MainArea][Cluster];
                            lister3.Add(outDL);
                        }
                        if (Tag.Trim() == "WX")
                        {
                            string outWX = Tag + " " + MainArea + " " + Cluster + " " + main_area[Tag][MainArea][Cluster];
                            lister4.Add(outWX);
                        }
                    }
                }
            }
            // Display output
            List<string> allList = new List<string>();
            allList.AddRange(lister1);
            allList.AddRange(lister2);
            allList.AddRange(lister3);
            allList.AddRange(lister4);

            Console.WriteLine();
            foreach (string rec in allList)
            {
                Console.Write(rec);
            }
 }

The above code shows record after the first lister1 List display all its records,
and all records of lister2 , lister3 and lister4.

All I need is to display the allList per row record of lister1 to lister4.

eg:

Lister1 has records
dog
cat
mouse

Lister2 has records
glass
hammer
jacket

Lister3 has records
pen
notebook
book

Lister4 has records
umbrella
bag
cellphone

my allList must display this collection of list per record.
the output must be like this:

allList must has a record:

dog, glass, pen, umbrella
cat, hammer, notebook, bag
mouse, jacket, book, cellphone


please help me on this sir.
your help is greatly appreciated..

- Jess</string></string></string></string></string></string></string></string></string></string>
Posted
Updated 18-May-11 19:43pm
v4

please let me know whether it helps or not

List<string> firstList = new List<string>();
            firstList.Add("dog");
            firstList.Add("cat");
            firstList.Add("mouse");

            List<string> secondList = new List<string>();
            secondList.Add("glass");
            secondList.Add("hammer");
            secondList.Add("jacket");

            List<string> thirdList = new List<string>();
            thirdList.Add("pen");
            thirdList.Add("notebook");
            thirdList.Add("book");

            List<string> fourthList = new List<string>();
            fourthList.Add("umbrella");
            fourthList.Add("bag");
            fourthList.Add("cellphone");
            fourthList.Add("lighter");
            fourthList.Add("wallet");

            List<List<string>> listCollection = new List<List<string>>();
            listCollection.Add(firstList);
            listCollection.Add(secondList);
            listCollection.Add(thirdList);
            listCollection.Add(fourthList);

            int maxListLength = 0;

            foreach (List<string> listItem in listCollection)
            {
                if (maxListLength < listItem.Count)
                {
                    maxListLength = listItem.Count;
                }
            }

            List<string> combineList = new List<string>();

            for (int ctr = 0; ctr < maxListLength; ctr++)
            {
                foreach (List<string> listItem in listCollection)
                {
                    if (listItem.Count > ctr)
                    {
                        combineList.Add(listItem[ctr]);
                    }
                }
            }

            foreach (string item in combineList)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
 
Share this answer
 
v3
Comments
Silver Lightning 19-May-11 1:49am    
Thanks for the help Sir senguptaamlan,
what would I update if my data has different no. of elements?
example is the FourthList has 5 records?

please help me
Thank you sir and God speed...
senguptaamlan 19-May-11 2:32am    
updated my solution....let me know whether this solves your problem....
Silver Lightning 19-May-11 4:31am    
great solution sir senguptaamlan,
just one more sir, how could I make the output of the combineList in 1 row per record (1st record from list1, list2, list3, list4), because it resulting to 1 line per record... is there anyway that it could be CONCATED?

EX:

the first record of combineList should display like this:

(1ST RECORD DISPLAY): DOG, GLASS, PEN, UMBRELLA
(2ND RECORD DISPLAY): CAT, HAMMER, NOTEBOOK, BAG
AND SO ON...

thanks for your help sir...
senguptaamlan 19-May-11 4:54am    
so you need a 4xn matrix ???
Silver Lightning 19-May-11 4:55am    
what is 4xn matrix sir?
Try that way..hope it will solve your problem

List<KeyValuePair<string, List<string>>> lstAll = new List<KeyValuePair<string, List<string>>>();

List<string> lst1 = new List<string> { "1", "2" };
List<string> lst2 = new List<string> { "3", "4" };
List<string> lst3 = new List<string> { "5", "6" };
List<string> lst4 = new List<string> { "7", "8", "9"};


lstAll.Add(new KeyValuePair<string, List<string>>(lst1.GetType().Name, lst1));
lstAll.Add(new KeyValuePair<string, List<string>>(lst2.GetType().Name, lst2));
lstAll.Add(new KeyValuePair<string, List<string>>(lst3.GetType().Name, lst3));
lstAll.Add(new KeyValuePair<string, List<string>>(lst4.GetType().Name, lst4));


foreach (KeyValuePair<string, List<string>> kv in lstAll)
{
    Response.Write(kv.Key + " has records <BR/>");
    foreach (string  item in kv.Value)
    {
        Response.Write(item + "<BR/>");
    }
}
 
Share this answer
 
v3
Comments
nit_singh 19-May-11 3:34am    
did it solved your problem?
Silver Lightning 19-May-11 3:45am    
hi sir nit_singh,

Thank you very much for your help sir nit_singh. it works, just one more concern sir,
how could I make them in 1 row (per record), I mean, the lstAll collection displays it per group.
that's my primary concern.
example: (based on above sample data)

the output should be: (from lstAll)
1, 3, 5, 7,
2, 4, 6, 8,
9,

please help me sir.
Silver Lightning 19-May-11 5:01am    
Thanks for your help too sir nit_singh.

I really appreciated that. It helps me too.

Have a nice day and God bless... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900