我們可以使用arr.Sum()
函數進行求和。但如果它是一個數組的數組。我們將如何添加所有值。 假設數據是 Array/List是[[1,2,3],[3,4,5],[5,4,3]]
如何使用LINQ獲得s1,全部第一個索引值之和,s2,第二個索引值之和等等。使用Linq添加索引位置陣列數值使用Linq
1
A
回答
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
一種可能的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;
}
}
}
相關問題
- 1. 如何陣列使用LINQ
- 2. 創建陣列使用LINQ
- 3. 檢索從數據表使用Linq的行索引Linq
- 4. 如何在LINQ查詢語言中使用索引/位置?
- 5. 當前的DataTable添加列使用LINQ
- 6. 使用linq添加項目列表
- 7. 添加使用LINQ到SQL
- 8. 添加和使用LINQ
- 9. 如何添加使用LINQ
- 10. 使用Linq將值添加到數據表中的列
- 11. 如何使用linq查詢使用索引從列表中檢索值?
- 12. 使用LINQ刪除空列使用LINQ
- 13. 使用linq查找索引位置,但考慮依賴於值的關係
- 14. 使用linq動態添加節點在xml中使用linq
- 15. 使用linq添加事件函數
- 16. 使用LINQ搜索
- 17. 搜索使用LINQ
- 18. 使用linq搜索
- 19. 使用LINQ從期望索引處的數組中選擇值
- 20. 如何使用LINQ獲取數組中最高值的索引?
- 21. LINQ到Db4o沒有使用索引
- 22. 在SelectMany中使用索引LINQ
- 23. LINQ的使用選擇和索引
- 24. 使用Linq按索引求和
- 25. 迭代LINQ的結果使用索引
- 26. LINQ/C#:Where&Foreach在列表/數組中使用索引
- 27. 使用LINQ相交2個陣列
- 28. 使用LINQ到總列數
- 29. 非LINQ陣列搜索
- 30. 添加一個索引LINQ查詢
因此,對於您的具體示例,您希望返回類似這樣的內容嗎? [[6],[12],[12]]? –
@Je不,輸出應該是[[9],[10],[11]] –
每行是否有相同數量的「列」? – mjwills