このブログではAffinger5を使用しています。
今回のテーマ
こんにちは、サルモリです。
今回の記事はGroupbyとタプルを組み合わせて、複数項目のグループに分ける記事です。
前回も同様に複数項目を分ける方法の記事を書きましたが、タプルを使用した例をもう少しあげるために、この記事を書きました!!
早速ソースコード例と出力をみていきましょう!

前回の記事はこちらです。
-
-
合わせて読みたいC# Linq Groupby グループ化する項目を複数指定する方法
このブログではAffinger5を使用しています。ブログテーマが気に入った方はコチラをクリック 今回のテーマ こんばんは、サルモリです。 今回のテーマは下記の記事について書いていきます。 今回のテーマ ...
続きを見る
Groupbyメソッドの使い方は下記の記事です。
-
-
合わせて読みたいC# Linqは便利なので使い方を覚えよう。GroupByを使ってグループ化しよう。
このブログではAffinger5を使用しています。ブログテーマが気に入った方はコチラをクリック 今回のテーマ こんばんは、サルモリです。 今回もLinqのメソッドを紹介してきます。今回紹介するメソッド ...
続きを見る
今回使用するクラス
このブログにおいて、ど定番のロボットクラスを使用します。
今回のロボットクラスは、各ロボット名に対し、メーカー、色、シリーズ、強さの項目を作りました。
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
----------------------------------------
沢山別れてましたが、3つ以上でも正しくグループ分けされていますね。
他のLinqの記事についてはこちら
まとめ
今回の記事はタプルとGroupbyを組み合わせると便利!という記事でした。
結構使用する場面あると思うので、覚えておくと便利ですよ!
それでは、お役に立てられていたら光栄です!それでは、また!!
