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);
}
}