4

I have a jagged array which I want to convert to a simple list.

int[][] jaggedArray = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};

I want to convert into a list

List<int> list = ?????

Fast way to convert a two dimensional array to a List ( one dimensional )

This question converts a two dimensional array to a list but this fails for jagged array.

Community
  • 1
  • 1
fhnaseer
  • 7,159
  • 16
  • 60
  • 112

2 Answers2

12

You might go with this:

List<int> list = jaggedArray.SelectMany(T => T).ToList();
AgentFire
  • 8,944
  • 8
  • 43
  • 90
6
var list = jaggedArray.SelectMany(x => x).ToList();
I4V
  • 34,891
  • 6
  • 67
  • 79