如何将4个列表集合合并到1个列表集合中,以及如何显示每4个列表开始的第一条记录. [英] How to merge 4 list collections in 1 list collection and display the first record of each 4 list onwards.

查看:78
本文介绍了如何将4个列表集合合并到1个列表集合中,以及如何显示每4个列表开始的第一条记录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

祝大家有美好的一天!
我想问一问如何将1个列表和所有记录中的4个列表集合合并
必须在4个列表集合的每行显示.

我的代码仅执行.AddRange,但仅将所有记录合并为一个整体.

这是我的示例代码方案:

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>

推荐答案

请让我知道它是否有帮助

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();


尝试这种方式..希望它将解决您的问题

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/>");
    }
}


这篇关于如何将4个列表集合合并到1个列表集合中,以及如何显示每4个列表开始的第一条记录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆