今回のテーマ
こんにちは、かっぱちゃんです。今回はGroupbyとタプルを組み合わせて、複数項目のグループに分ける記事です。
前回も複数項目を分ける方法の記事を書きましたが、タプルを使用した方が直観的に分かりやすく使いやすいと思うので、改めて記事にしようと思いました。
早速ソースコードとどのように出力されるかみていきましょう!!!

前回の記事
-
-
合わせて読みたいC# Linq Groupby グループ化する項目を複数指定する方法
今回のテーマContents1 今回のテーマ2 今回使用するクラス3 複数項目でグループ化する方法4 複数項目でグループ化する方法(タプルを利用する方法)5 まとめ こんばんは、かっぱちゃんです。 今 ...
続きを見る
Groupbyメソッドの使い方は下記の記事です。
-
-
合わせて読みたいC# Linqの使い方。GroupByを使ってグループ化しよう。
今回のテーマContents1 今回のテーマ2 GroupByについて2.1 GroupBy3 Linqメソッドの解説の流れ4 定義するクラスについて5 GroupByの使い方6 2つ以上指定する方法 ...
続きを見る
今回使用するクラス
このブログにおいて、ど定番のロボットクラスを使用します。
今回のロボットクラスは、各ロボット名に対し、メーカー、色、シリーズ、強さの項目を作りました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Robot { string name, maker, color; int series,strength; public Robot(string _name,string _maker,string _color,int _series,int _strength) { name = _name; maker = _maker; color = _color; series = _series; strength = _strength; } public string getName() { return name; } public string getMaker() { return maker; } public string getColor() { return color; } public int getSeries() { return series; } public int getStrength() { return strength; } } |
タプルを使用して、2項目のグループ分けをしてみよう
早速、タプルで2項目を指定してグループ分けしてみましょう。このタプルとGroupbyの組み合わせの破壊力を味わって下さい。
破壊力を感じてほしいので、いつも以上にロボット名の追加を頑張りました。強さはかっぱちゃんの主観で設定しています。
まずはメーカー別、色別で分けてみましょう!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Airman", "Dr Wily", "Blue", 2, 3)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); robotlist.Add(new Robot("Hardman", "Dr Wily", "Blue", 3, 4)); robotlist.Add(new Robot("Flashman", "Dr Wily", "Blue", 2, 2)); var robotslists = robotlist.GroupBy(n => (n.getMaker(),n.getColor())); foreach(var rlist in robotslists) { foreach(var robot in rlist) { Console.WriteLine("Name:" + robot.getName() + " Maker:" + robot.getMaker() + " Color:" + robot.getColor()); } Console.WriteLine("----------------------------------------"); } } } |
出力結果
Name:Rockman Maker:Dr Light Color:Blue
----------------------------------------
Name:Rollchan Maker:Dr Light Color:Pink
----------------------------------------
Name:Heatman Maker:Dr Wily Color:Red
Name:Fireman Maker:Dr Wily Color:Red
Name:Quickman Maker:Dr Wily Color:Red
Name:Sparkman Maker:Dr Wily Color:Red
----------------------------------------
Name:Iceman Maker:Dr Wily Color:Blue
Name:Airman Maker:Dr Wily Color:Blue
Name:Shadowman Maker:Dr Wily Color:Blue
Name:Hardman Maker:Dr Wily Color:Blue
Name:Flashman Maker:Dr Wily Color:Blue
----------------------------------------
Name:Snakeman Maker:Dr Wily Color:Green
Name:Woodman Maker:Dr Wily Color:Green
----------------------------------------
凄いですよね!Groupbyでグループ別にしたい項目をタプルに設定するだけで出来てしまいます!素晴らしい!
1 |
robotlist.GroupBy(n => (n.getMaker(),n.getColor())); |
シリーズ、強さ別にも分けてみましょうか!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Airman", "Dr Wily", "Blue", 2, 3)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); robotlist.Add(new Robot("Hardman", "Dr Wily", "Blue", 3, 4)); robotlist.Add(new Robot("Flashman", "Dr Wily", "Blue", 2, 2)); var robotslists = robotlist.GroupBy(n => (n.getSeries(),n.getStrength())); foreach(var rlist in robotslists) { foreach(var robot in rlist) { Console.WriteLine("Name:" + robot.getName() + " Series:" + robot.getSeries() + " Strength:" + robot.getStrength()); } Console.WriteLine("----------------------------------------"); } } } |
出力結果
Name:Rockman Series:1 Strength:5
----------------------------------------
Name:Rollchan Series:1 Strength:1
----------------------------------------
Name:Heatman Series:2 Strength:2
Name:Flashman Series:2 Strength:2
----------------------------------------
Name:Fireman Series:1 Strength:3
----------------------------------------
Name:Iceman Series:1 Strength:4
----------------------------------------
Name:Quickman Series:2 Strength:5
----------------------------------------
Name:Airman Series:2 Strength:3
Name:Woodman Series:2 Strength:3
----------------------------------------
Name:Shadowman Series:3 Strength:5
----------------------------------------
Name:Snakeman Series:3 Strength:3
----------------------------------------
Name:Sparkman Series:3 Strength:4
Name:Hardman Series:3 Strength:4
----------------------------------------
沢山グループが出来てしまいましたが、正しくグループ分けされていますね!

タプルを使用して、3項目のグループ分けをしてみよう
3項目以上も同様にタプルに複数個指定するだけで大丈夫です。
メーカー、色、シリーズ別に分けてみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { List<Robot> robotlist = new List<Robot>(); robotlist.Add(new Robot("Rockman", "Dr Light", "Blue", 1, 5)); robotlist.Add(new Robot("Rollchan", "Dr Light", "Pink", 1, 1)); robotlist.Add(new Robot("Heatman", "Dr Wily", "Red", 2, 2)); robotlist.Add(new Robot("Fireman", "Dr Wily", "Red", 1, 3)); robotlist.Add(new Robot("Iceman", "Dr Wily", "Blue", 1, 4)); robotlist.Add(new Robot("Quickman", "Dr Wily", "Red", 2, 5)); robotlist.Add(new Robot("Airman", "Dr Wily", "Blue", 2, 3)); robotlist.Add(new Robot("Shadowman", "Dr Wily", "Blue", 3, 5)); robotlist.Add(new Robot("Snakeman", "Dr Wily", "Green", 3, 3)); robotlist.Add(new Robot("Woodman", "Dr Wily", "Green", 2, 3)); robotlist.Add(new Robot("Sparkman", "Dr Wily", "Red", 3, 4)); robotlist.Add(new Robot("Hardman", "Dr Wily", "Blue", 3, 4)); robotlist.Add(new Robot("Flashman", "Dr Wily", "Blue", 2, 2)); var robotslists = robotlist.GroupBy(n => (n.getMaker(),n.getColor(),n.getSeries())); foreach(var rlist in robotslists) { foreach(var robot in rlist) { Console.WriteLine("Name:" + robot.getName() + " Maker:" + robot.getMaker() + " Color:" + robot.getColor() + " Series:" + robot.getSeries()); } Console.WriteLine("----------------------------------------"); } } } |
出力結果
Name:Rockman Maker:Dr Light Color:Blue Series:1
----------------------------------------
Name:Rollchan Maker:Dr Light Color:Pink Series:1
----------------------------------------
Name:Heatman Maker:Dr Wily Color:Red Series:2
Name:Quickman Maker:Dr Wily Color:Red Series:2
----------------------------------------
Name:Fireman Maker:Dr Wily Color:Red Series:1
----------------------------------------
Name:Iceman Maker:Dr Wily Color:Blue Series:1
----------------------------------------
Name:Airman Maker:Dr Wily Color:Blue Series:2
Name:Flashman Maker:Dr Wily Color:Blue Series:2
----------------------------------------
Name:Shadowman Maker:Dr Wily Color:Blue Series:3
Name:Hardman Maker:Dr Wily Color:Blue Series:3
----------------------------------------
Name:Snakeman Maker:Dr Wily Color:Green Series:3
----------------------------------------
Name:Woodman Maker:Dr Wily Color:Green Series:2
----------------------------------------
Name:Sparkman Maker:Dr Wily Color:Red Series:3
----------------------------------------
また沢山別れましたが、正しくグループ分けされていますね。
他のLinqの記事についてはこちら
まとめ
今回の記事はタプルとGroupbyを組み合わせると便利!という記事でした。
結構使用する場面あると思うので、覚えておくと便利ですよ!
それでは、お役に立てられていたら光栄です!それでは、また!!

-
-
合わせて読みたいC# Linqを勉強するならこれだけは読んでほしいオススメの本3選 オススメの参考書
C#を勉強するためのオススメの本を紹介Contents1 C#を勉強するためのオススメの本を紹介1.1 C#の基礎を学ぶなら「やさしいC#」1.2 C#の全体像を学ぶなら「独習C#」1.3 C#をさら ...
続きを見る