Click here to Skip to main content
11,647,037 members (79,436 online)
Rate this: bad
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 :



How make it with
Dictionary<string, List<string>> axxx = new Dictionary<string, List<string>>();
Posted 29-Apr-12 10:09am
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.
SAKryukov at 29-Apr-12 19:16pm
Reason for my vote of 2
Ambiguous. Isn't this obvious? Use some simple logic.
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
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>();
//The contents of refDic will look like
//Key - auto
//Value - List<string> of
//Key - bus
//Value - List<string> of
SAKryukov at 30-Apr-12 19:15pm
Should provide some idea to OP, my 5.
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 510
1 Sergey Alexandrovich Kryukov 423
2 OriginalGriff 398
3 CPallini 339
4 DamithSL 300
0 OriginalGriff 1,253
1 994
2 DamithSL 896
3 Sergey Alexandrovich Kryukov 833
4 CPallini 720

Advertise | Privacy | Mobile
Web02 | 2.8.150804.2 | Last Updated 29 Apr 2012
Copyright © CodeProject, 1999-2015
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