回答編集履歴
1
.NET 5未満でも実行できるように変更
answer
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
LINQを使って次のようにスッキリに出来るかと思います。
|
1
|
+
LINQを使って次のようにスッキリに出来るかと思います。
|
2
2
|
|
3
3
|
```csharp
|
4
4
|
using System;
|
5
5
|
using System.Linq;
|
6
6
|
using System.Collections.Generic;
|
7
|
-
|
7
|
+
|
8
8
|
public class Program
|
9
9
|
{
|
10
10
|
public static void Main()
|
11
11
|
{
|
12
12
|
var array1 = new string[,]{ {"a","1"}, {"b", "2"}};
|
13
|
-
var array2 = new string[,]{ {"a","
|
13
|
+
var array2 = new string[,]{ {"a","3"}, {"c", "3"}};
|
14
|
-
|
14
|
+
|
15
15
|
foreach(var t in Program.ToTuples(array1).Concat(Program.ToTuples(array2))
|
16
16
|
.GroupBy(row => row.Item1)
|
17
|
-
.Select(t => (t.Key, t.Sum(row => row.Item2))) ) {
|
17
|
+
.Select(t => Tuple.Create(t.Key, t.Sum(row => row.Item2))) ) {
|
18
18
|
Console.WriteLine(t.Item1 + " " + t.Item2);
|
19
19
|
}
|
20
20
|
}
|
21
|
-
static IEnumerable<
|
21
|
+
static IEnumerable<Tuple<string, int>>ToTuples(string[,] array) {
|
22
22
|
for (int i=0; i < array.GetLength(0); i++) {
|
23
|
-
yield return (array[i,0], int.Parse(array[i,1]));
|
23
|
+
yield return Tuple.Create(array[i,0], int.Parse(array[i,1]));
|
24
24
|
}
|
25
25
|
}
|
26
|
-
|
26
|
+
|
27
27
|
}
|
28
28
|
|
29
29
|
```
|