Click here to Skip to main content
13,350,708 members (56,654 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have two lists Lst1<string> and Lst2<string>

[__u__]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 11:09am
SAKryukov 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 29-Apr-12 19:16pm
Reason for my vote of 2
Ambiguous. Isn't this obvious? Use some simple logic.
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 R. 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

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 30-Apr-12 19:15pm
Should provide some idea to OP, my 5.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 29 Apr 2012
Copyright © CodeProject, 1999-2018
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