2017-07-06 51 views
1

我們可以使用arr.Sum()函數進行求和。但如果它是一個數組的數組。我們將如何添加所有值。 假設數據是 Array/List是[[1,2,3],[3,4,5],[5,4,3]] 如何使用LINQ獲得s1,全部第一個索引值之和,s2,第二個索引值之和等等。使用Linq添加索引位置陣列數值使用Linq

+0

因此,對於您的具體示例,您希望返回類似這樣的內容嗎? [[6],[12],[12]]? –

+0

@Je不,輸出應該是[[9],[10],[11]] –

+0

每行是否有相同數量的「列」? – mjwills

回答

6

如果你想與的LINQ的幫助下,總結列的值

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

int maxCol = source.Max(item => item.Length); 

var colsSum = Enumerable 
    .Range(0, maxCol) 
    .Select(index => source.Sum(item => item.Length > index ? item[index] : 0)) 
    .ToArray(); // let's meaterialize into an array 

測試:

Console.Write(string.Join(", ", colsSum)); 

結果:

9, 10, 11 

總結林ES的價值觀更容易:

// [6, 12, 12] 
var linesSum = source 
    .Select(item => item.Sum()) 
    .ToArray(); 

如果你想總和:

// 30 
var total = source 
    .Select(item => item.Sum()) 
    .Sum(); 

// 30 
var total = source 
    .SelectMany(item => item) 
    .Sum(); 
0

Aggregate使用組合Zip

var arrays = new[] 
{ 
    new[] { 1, 2, 3 }, 
    new[] { 3, 4, 5 }, 
    new[] { 5, 4, 3 } 
}; 

var result = 
    arrays.Aggregate(Enumerable.Repeat(0, 3), 
        (total, array) => total.Zip(array, (sum, current) => sum + current)); 

// result = { 9, 10, 11 } 

Enumerable<T>.Zip執行提供的具有相同索引的項目的功能。

+0

如果其中一行具有與其他行不同數量的條目,這是否可以工作? – mjwills

+0

是的,只有'.Zip'函數會計算內部數組,直到它到達其中一個的末尾。所以如果最小數組有2個項目,那麼它會計算兩個項目。 OP沒有提到這種限制 – Fabio

0

一種可能的LINQ爲基礎的方法(將處理每一行中的變量數列):

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace Test 
{ 
    public class Program 
    { 
     private static IEnumerable<int> GetTotalsPerColumn(int[][] inputData) 
     { 
      var data = inputData.SelectMany(z => 
      { 
       return z.Select((item, index) => new { item, index }); 
      }) 
       .GroupBy(z => z.index) 
       .OrderBy(z => z.Key) 
       .Select(y => y.Select(z => z.item).Sum() 
      ); 

      return data; 
     } 

     static void Main(string[] args) 
     { 
      var inputData = new[] { 
         new[] { 1, 2, 3, 5}, 
         new[] { 3, 4, 5, 6}, 
         new[] { 5, 4, 3}, 
        }; 

      var values = GetTotalsPerColumn(inputData); 

      foreach (var value in values) 
      { 
       Console.WriteLine(value); 
      } 

      Console.ReadLine(); 
     } 
    } 
} 

如果你很高興,以避免LINQ,這是你可以考慮另一種方法。 GetTotalsPerColumn填充Dictionary其中鍵是列號,並且該值是總和。

using System; 
using System.Collections.Generic; 

namespace Test 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      var inputData = new[] { 
        new[] { 1, 2, 3, 5}, 
        new[] { 3, 4, 5, 6}, 
        new[] { 5, 4, 3}, 
       }; 

      var values = GetTotalsPerColumn(inputData); 

      foreach (var value in values) 
      { 
       Console.WriteLine(value.Key + " - " + value.Value); 
      } 

      Console.ReadLine(); 
     } 

     private static Dictionary<int, int> GetTotalsPerColumn(int[][] inputData) 
     { 
      var values = new Dictionary<int, int>(); 

      foreach (var line in inputData) 
      { 
       for (int i = 0; i < line.Length; i++) 
       { 
        int tempValue; 

        values.TryGetValue(i, out tempValue); 
        tempValue += line[i]; 
        values[i] = tempValue; 
       } 
      } 
      return values; 
     } 
    } 
} 
+0

問題是關於_using LINQ_;) – Fabio

+0

好點@Fabio - 謝謝。我已經添加了LINQ和非LINQ版本。 – mjwills

+0

在行'return z.Select(item => new {item,index = index ++});'你不需要通過你自己來計算索引。 'Select'方法有兩個參數item和index的重載。 - 'return z.Select((item,index)=> new {item,index});' – Fabio