2017-07-06 61 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