C# 使用Linq添加数组的数组的索引位置值
我们可以用arr.sum函数求和。但如果它是一个数组的数组。我们将如何添加所有值。 假设数据是 数组/列表为[[1,2,3],[3,4,5],[5,4,3]] 如何使用LINQ获得s1、所有第一个索引值之和、s2、第二个索引值之和等等。如果要借助LINQ对列的值求和: 测试: 结果:C# 使用Linq添加数组的数组的索引位置值,c#,arrays,linq,C#,Arrays,Linq,我们可以用arr.sum函数求和。但如果它是一个数组的数组。我们将如何添加所有值。 假设数据是 数组/列表为[[1,2,3],[3,4,5],[5,4,3]] 如何使用LINQ获得s1、所有第一个索引值之和、s2、第二个索引值之和等等。如果要借助LINQ对列的值求和: 测试: 结果: 9, 10, 11 汇总行的值更容易: // [6, 12, 12] var linesSum = source .Select(item => item.Sum()) .ToArray();
9, 10, 11
汇总行的值更容易:
// [6, 12, 12]
var linesSum = source
.Select(item => item.Sum())
.ToArray();
如果您想要总金额:
或
如果要借助Linq对列的值求和,请执行以下操作:
测试:
结果:
9, 10, 11
汇总行的值更容易:
// [6, 12, 12]
var linesSum = source
.Select(item => item.Sum())
.ToArray();
如果您想要总金额:
或
使用聚合和压缩的组合
Enumerable.Zip使用相同索引的项执行提供的函数。使用聚合和Zip的组合
Enumerable.Zip使用相同索引的项执行提供的函数。一种可能的基于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的方法,它将处理每行中的可变列数:
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();
}
}
}
那么,对于你的具体例子,你希望返回这样的东西?[[6]、[12]、[12]]?@Je不,输出应该是[[9]、[10]、[11]]每行的列数是否相同?因此,对于您的特定示例,您希望返回类似的内容?[[6]、[12]、[12]]?@Je否,输出应该是[[9]、[10]、[11]]每行的列数是否相同?问题是关于使用LINQ;很好的观点@Fabio-谢谢。我添加了LINQ和非LINQ版本;你不需要自己计算指数。Select方法具有带有两个参数项和索引的重载。-返回z.Selectitem,index=>new{item,index};问题是关于使用LINQ;很好的观点@Fabio-谢谢。我添加了LINQ和非LINQ版本;你不需要自己计算指数。Select方法具有带有两个参数项和索引的重载。-返回z.Selectitem,index=>new{item,index};如果其中一行的条目数与其他行的条目数不同,这是否可行?是的,仅此而已。Zip函数将计算内部数组,直到到达其中一行的末尾。所以,若最小的数组有两个项目,那个么它将计算两个项目。OP没有提到这种限制。如果其中一行的条目数与其他行的条目数不同,这种限制是否有效?是的,仅此而已。Zip函数将计算内部数组,直到到达其中一行的末尾。所以,若最小的数组有两个项目,那个么它将计算两个项目。OP没有提到这种限制
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 }
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();
}
}
}
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;
}
}
}