Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Framework
I have two lists Lst1 and Lst2
 

 
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
Dictionary<string, List<string>> axxx = new Dictionary<string, List<string>>();
Posted 29-Apr-12 10:09am
Radzhab1.1K
Comments
SAKryukov at 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
SAKryukov at 29-Apr-12 19:16pm
   
Reason for my vote of 2
Ambiguous. Isn't this obvious? Use some simple logic.
--SA
VJ Reddy at 29-Apr-12 21:08pm
   
Agreed. The question is not clear. However, I have added a solution with some suggestions. Thank you.
Manfred R. Bihy at 7-May-12 8:28am
   
Somehow can't see your solution. What happend to it?
VJ Reddy at 7-May-12 10:25am
   
But I could see Solution 2, which was posted by me.
Thank you.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  Permalink  
v2
Comments
SAKryukov at 30-Apr-12 19:15pm
   
Should provide some idea to OP, my 5.
--SA
VJ Reddy at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 269
1 Peter Leow 205
2 OriginalGriff 200
3 _Amy 190
4 Andreas Gieriet 180
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,412
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100