練習問題を始める前に
こんにちは、かっぱちゃんです。Linqの練習問題を載せていきます。今回の記事は中級編2です。
イメージがしにくいGroupByの問題を沢山解いてマスターしていきましょう!!
下記に演習問題をまとめてあります。他の問題もどんどん解いてみてください。
他の演習問題はこちら
LinqのGroupByは比較的理解しにくいと思いますが、頑張って知識を付けていってください!

演習問題を解く前に下記のソースコードをコピーして貼り付け
下記のソースコードを貼り付けてください。Program.csファイルでもRobot.csファイルを作成して貼り付けても問題ありません。
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 |
//下記のロボットクラスをコピーして、貼り付けて下さい。 class Robot { string name, maker, color; int strength; public Robot(string _name,string _maker,string _color,int _strength) { name = _name; maker = _maker; color = _color; strength = _strength; } public string getName() { return name; } public string getMaker() { return maker; } public string getColor() { return color; } public int getstrength() { return strength; } } |

第1問
ロボット名をメーカー別に出力してください。
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 |
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", "Mr Light", "Blue", 5)); robotlist.Add(new Robot("Quickman", "Mr Wily", "Red", 5)); robotlist.Add(new Robot("Airman", "Mr Wily", "Blue", 3)); robotlist.Add(new Robot("Rollchan", "Mr Light", "Pink", 1)); robotlist.Add(new Robot("Woodman", "Mr Wily", "Green", 3)); robotlist.Add(new Robot("Bubbleman", "Mr Wily", "Green", 4)); robotlist.Add(new Robot("Fireman", "Mr Wily", "Red", 3)); robotlist.Add(new Robot("Iceman", "Mr Wily", "Blue", 4)); robotlist.Add(new Robot("Heatman", "Mr Wily", "Red", 2)); //**************ここからコードを書いて下さい************** var makerrobots = //**************ここまでコードを書いて下さい************** foreach (var robots in makerrobots) { foreach(var robot in robots) { Console.WriteLine(robot.getName()); } Console.WriteLine("-------------"); } } } |
出力結果
Rockman
Rollchan
-------------
Quickman
Airman
Woodman
Bubbleman
Fireman
Iceman
Heatman
-------------

第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 |
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", "Mr Light", "Blue", 5)); robotlist.Add(new Robot("Quickman", "Mr Wily", "Red", 5)); robotlist.Add(new Robot("Airman", "Mr Wily", "Blue", 3)); robotlist.Add(new Robot("Rollchan", "Mr Light", "Pink", 1)); robotlist.Add(new Robot("Woodman", "Mr Wily", "Green", 3)); robotlist.Add(new Robot("Bubbleman", "Mr Wily", "Green", 4)); robotlist.Add(new Robot("Fireman", "Mr Wily", "Red", 3)); robotlist.Add(new Robot("Iceman", "Mr Wily", "Blue", 4)); robotlist.Add(new Robot("Heatman", "Mr Wily", "Red", 2)); //**************ここからコードを書いて下さい************** var colorrobots = //**************ここまでコードを書いて下さい************** foreach (var robots in colorrobots) { foreach(var robot in robots) { Console.WriteLine(robot.getName() + ":" + robot.getColor()); } Console.WriteLine("-------------"); } } } |
出力結果
Rockman:Blue
Airman:Blue
Iceman:Blue
-------------
Quickman:Red
Fireman:Red
Heatman:Red
-------------
Rollchan:Pink
-------------
Woodman:Green
Bubbleman:Green
-------------
第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 |
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", "Mr Light", "Blue", 5)); robotlist.Add(new Robot("Quickman", "Mr Wily", "Red", 5)); robotlist.Add(new Robot("Airman", "Mr Wily", "Blue", 3)); robotlist.Add(new Robot("Rollchan", "Mr Light", "Pink", 1)); robotlist.Add(new Robot("Woodman", "Mr Wily", "Green", 3)); robotlist.Add(new Robot("Bubbleman", "Mr Wily", "Green", 4)); robotlist.Add(new Robot("Fireman", "Mr Wily", "Red", 3)); robotlist.Add(new Robot("Iceman", "Mr Wily", "Blue", 4)); robotlist.Add(new Robot("Heatman", "Mr Wily", "Red", 2)); //**************ここからコードを書いて下さい************** var colorwilyrobots = //**************ここまでコードを書いて下さい************** foreach (var robots in colorwilyrobots) { foreach(var robot in robots) { Console.WriteLine(robot.getName() + ":" + robot.getColor()); } Console.WriteLine("-------------"); } } } |
出力結果
Quickman:Red
Fireman:Red
Heatman:Red
-------------
Airman:Blue
Iceman:Blue
-------------
Woodman:Green
Bubbleman:Green
-------------
第4問
ロボットリストを強さ別に並べてください。また、出力は強さ順に並べてください。
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 |
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", "Mr Light", "Blue", 5)); robotlist.Add(new Robot("Quickman", "Mr Wily", "Red", 5)); robotlist.Add(new Robot("Airman", "Mr Wily", "Blue", 3)); robotlist.Add(new Robot("Rollchan", "Mr Light", "Pink", 1)); robotlist.Add(new Robot("Woodman", "Mr Wily", "Green", 3)); robotlist.Add(new Robot("Bubbleman", "Mr Wily", "Green", 4)); robotlist.Add(new Robot("Fireman", "Mr Wily", "Red", 3)); robotlist.Add(new Robot("Iceman", "Mr Wily", "Blue", 4)); robotlist.Add(new Robot("Heatman", "Mr Wily", "Red", 2)); //**************ここからコードを書いて下さい************** var strengthrobots = //**************ここまでコードを書いて下さい************** foreach (var robots in strengthrobots) { foreach(var robot in robots) { Console.WriteLine(robot.getName() + ":" + robot.getstrength()); } Console.WriteLine("-------------"); } } } |
出力結果
Rockman:5
Quickman:5
-------------
Bubbleman:4
Iceman:4
-------------
Airman:3
Woodman:3
Fireman:3
-------------
Heatman:2
-------------
Rollchan:1
-------------

まとめ
今回は主にGroupByメソッドの問題集でした。Linqの中でもわかりにくいメソッドですが、この機会に覚えちゃってください。
比較的使う場面が多いので、何回も問題にチャレンジして、自分の知識にしてください。

新しい演習問題の記事を作成しました。
こちらもCHECK
-
-
C# Linq練習問題集 ~Linqをマスターしよう~ 中級3
演習問題を始める前にContents1 演習問題を始める前に1.1 第1問1.2 第2問1.3 第3問1.4 第4問1.5 第5問1.6 まとめ こんにちは、かっぱちゃんです。今回の記事は中級編の練習 ...
続きを見る
他の演習問題はこちら