GroupByメソッド③
GroupByメソッドはクラスコレクション以外、通常のarrayコレクションでも使用可能です。
List<string[]> lst = new List<string[]> { new string[]{ "1", "25", "5", "3000"}, new string[]{ "1", "31", "10", "4000"}, new string[]{ "2", "31", "98", "12800"}, new string[]{ "1", "25", "11", "250"} }; var res = lst.GroupBy(x =>Tuple.Create(x[0], x[1] ));
Dim lst As New List(Of String()) From { New String() {"1", "25", "5", "3000"}, New String() {"1", "31", "10", "4000"}, New String() {"2", "31", "98", "12800"}, New String() {"1", "25", "11", "250"}} Dim res = lst.GroupBy(Function(x) Tuple.Create(x(0), x(1)))
GroupByメソッドにより、集約したコレクションの結果を使って集計することが簡単に出来ます。
var res = lst.GroupBy((x) => Tuple.Create(x[0],x[1])) .Select((y) => new { KaisyaCD = y.Key.Item1, SyainCD = y.Key.Item2, SumValue = y.Sum((s) => int.Parse(s[2])) } ); foreach r In res Console.WriteLine(r.KaisyaCD + ":" + r.SyainCD + " " + r.SumValue); Next
Dim res = lst .GroupBy(Function(s) Tuple.Create(s(0), s(1))).Select( Function(y) New With { .KaisyaCD = y.Key.Item1, .SyainCD = y.Key.Item2, .SumValue = y.Sum(Function(s) Integer.Parse(s(2))) }) For Each r In res Console.WriteLine(r.KaisyaCD & ":" & r.SyainCD & " " & r.SumValue) Next
1:25 16 1:31 10 2:31 98