Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have two lists Lst1<string> and Lst2<string>



Lst1                                         Lst2

auto - 1q                                    n122
auto - 2q                                    n341
auto - 3q                                    n461

bus- 1q                                      n132
bus- 2q                                      n441
bus- 3q                                      n761



I want that's become :

auto
    n122
    n341
    n461


bus
    n132
    n441
    n761


How make it with
C#
Dictionary<string, List<string>> axxx = new Dictionary<string, List<string>>();
Posted
Comments
Sergey Alexandrovich Kryukov 29-Apr-12 19:15pm    
This is not a valid question unless you explain the purpose of it. What features of a dictionary you hope to use here, for what purpose. Without knowing it, there is no an unambiguous answer.
--SA
Sergey Alexandrovich Kryukov 29-Apr-12 19:16pm    
Reason for my vote of 2
Ambiguous. Isn't this obvious? Use some simple logic.
--SA
VJ Reddy 29-Apr-12 21:08pm    
Agreed. The question is not clear. However, I have added a solution with some suggestions. Thank you.
Manfred Rudolf Bihy 7-May-12 8:28am    
Somehow can't see your solution. What happend to it?
VJ Reddy 7-May-12 10:25am    
But I could see Solution 2, which was posted by me.
Thank you.

1 solution

The comment by SAKryukov to the question is correct.
To combine two Lists, basically a relationship is required to know which item of List2 is to be related to which item of List1. In the question auto - 1q, auto - 2q, auto - 3q are given. Assuming that 1q, 2q, 3q refer to the first, second, third items of List2 respectively, they can be related accordingly. But then 1q, 2q, 3q are also given for bus items in List1. Then again the same items of List2 are related to these. Hence, I think it can be related by giving proper index of the corresponding item in List2 to the item in List1 as shown below:
C#
void Main()
{
    List<string> list1 = new List<string>(){"auto - 1","auto - 2","auto - 3","bus - 4","bus - 5","bus - 6"};
    List<string> list2 = new List<string>(){"n122","n341","n461","n132","n441","n761"};
    Dictionary<string, List<string>> refDic = new Dictionary<string, List<string>>();
    foreach(string item in list1){
        string[] keyInd=item.Split(new char[]{'-'});
        int ind = int.Parse(keyInd[1].Trim())-1;
        string refValue = ind < list2.Count ?
                list2[ind] : string.Empty;
        List<string> refValues;
        if (!refDic.TryGetValue(keyInd[0].Trim(),out refValues)){
            refValues = new List<string>();
            refDic.Add(keyInd[0].Trim(),refValues);}
        refValues.Add(refValue);
    }
}
//The contents of refDic will look like
//Key - auto
//Value - List<string> of
//n122
//n341
//n461
//
//Key - bus
//Value - List<string> of
//n132
//n441
//n761
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 30-Apr-12 19:15pm    
Should provide some idea to OP, my 5.
--SA
VJ Reddy 30-Apr-12 20:12pm    
Thank you, SA.

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