0

I have two List:

var listA = new List<List<int>> {
    new List<int> { 1, 2, 3, 4 },
    new List<int> { 4, 5, 6, 7 },
    new List<int> { 6, 7, 8, 9 }
};

var listB = new List<string> { "number1", "number2", "number3", "number4" };

How to convert them to dictionary with listA is value, listB is key?

S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
le cuong
  • 11
  • 1

1 Answers1

0

You can use ToDictionary method as following:

var listA = new List<List<int>> {
    new List<int> { 1, 2, 3, 4 },
    new List<int> { 4, 5, 6, 7 },
    new List<int> { 6, 7, 8, 9 }
};

var listB = new List<string> { "number1", "number2", "number3", "number4" };

var dic = listB.Select((k, i) => new { key = k, value = listA[i] })
          .ToDictionary(x => x.key, x => x.value);

//now, dic["number1"] is List<int> { 1, 2, 3, 4 }
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61