Categorise a static pair/value list collection RRS feed

  • Question

  • User1909155429 posted

    At the moment i have a static list like such: 1,2,3,one,two,three,four,a,b,c,d,e  that are bound to a dropdownlist as text/value pairs.

    I now want to split the list  into seperate indentities with corresponding headings that describe the seperate identities to be bound to a listview control.

    For example

    Numbers    Strings

    1                    one

    2                   two

    3                   three




    Monday, May 12, 2014 8:11 AM


  • User-1818759697 posted


    For this situation, you could try to use the split function to separate the value into two, then add them to the corresponding list:

     var list = new List<KeyValuePair<string, int>>();
                list.Add(new KeyValuePair<string, int>("Cat", 1));
                list.Add(new KeyValuePair<string, int>("Dog", 2));
                list.Add(new KeyValuePair<string, int>("Rabbit", 4));
                List<string> list1 = new List<string>();
                List<string> list2 = new List<string>();
                foreach(var element in list)
                    string[] str=element.ToString().Split(',');//separate the value into [Cat   and  1]
                    list1.Add(str[0].Substring(1));//get the cat
                    list2.Add(str[1].Substring(0,2));//get the number

    With the above codes, you could get the string value in list1 and int value in list2.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2014 10:22 PM