13,350,708 members (56,654 online)
Rate this:
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 :

```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 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.
--SA
SAKryukov 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 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.

Rate this:

## 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
//n122
//n341
//n461
//
//Key - bus
//Value - List<string> of
//n132
//n441
//n761```
v2
SAKryukov 30-Apr-12 19:15pm

Should provide some idea to OP, my 5.
--SA
VJ Reddy 30-Apr-12 20:12pm

Thank you, SA.

Top Experts
Last 24hrsThis month
 OriginalGriff 285 debasish mishra 139 CPallini 120 Jochen Arndt 111 CHill60 50
 OriginalGriff 3,796 Maciej Los 1,185 Jochen Arndt 1,156 Karthik Bangalore 1,111 CPallini 992