2010-06-16 73 views

回答

0
IEnumerable<IEnumerable<int>> columns = values 
    .SelectMany((row, ri) => row 
    .Select((x, ci) => new {cell = x, ci, ri})) 
    .GroupBy(z => z.ci) 
    .Select(g => g.Select(z => z.cell)); 

一些注意事項:

  • 這不保持空白來自不同大小陣列的空間(記住 - 鋸齒狀)。
  • rowindex(ri)未使用,可以刪除。
  • 的rowIndex可以使用,如果需要
1
var cols = values.SelectMany(v=>v.Select(c=>c)) 
+0

在我看來,這個變平的陣列,有沒有辦法讓列作爲陣列生成emptyspace值? – CoGo 2010-06-16 20:27:35

1

再添加一個行:

int[][] values; 
.... 
var rows = from row in values select row; 
var cols = rows.SelectMany(x => x); 
+0

在我看來,這使數組變平了,有沒有辦法讓列變成數組? – CoGo 2010-06-16 20:27:12

-1

你也可以做這樣的:

 int[][] values = new int[5][]; 
     values[0] = new int[5] { 1, 2, 3, 4, 5 }; 
     values[1] = new int[5] { 1, 2, 3, 4, 5 }; 
     values[2] = new int[5] { 1, 2, 3, 4, 5 }; 
     values[3] = new int[5] { 1, 2, 3, 4, 5 }; 
     values[4] = new int[5] { 1, 2, 3, 4, 5 }; 

     var rows = from r in values where r[0] == 1 select r[0]; 

     //two options here for navigating each row or navigating one row 

     var rows = from r in values[0] where r == 1 select r;