今回もLINQの記事です!
LINQ便利すぎ!
LINQ Sum, Average, Min, Maxなど計算系解説
プログラミングの勉強によくありがちな、学校のテストの点数で実験してみます!
以下ソースです!
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class LinqTest : MonoBehaviour { List<Student> humans = new List<Student>(); int id = 0; void Start () { MakeStudent("たろう", true, 90, 80); MakeStudent("じろう", true, 70, 70); MakeStudent("はなこ", false, 100, 100); MakeStudent("さとこ", false, 50, 30); humans.ForEach(h => Debug.Log("ID:"+h.id+", 名前:"+h.name+", 性別:"+h.gender.ToString()+", 英語点数:"+h.englishScore+", 数学点数:"+h.mathScore)); } void MakeStudent(string name, bool isMan, int englishScore, int mathScore) { humans.Add(new Student(id++, name, isMan, englishScore, mathScore)); } } public class Student { public int id; public string name; public enum Gender { Male, Female } public Gender gender; public int englishScore; public int mathScore; // コンストラクターでID, 名前, 性別, 英語の点数, 数学の点数を設定 public Student(int id, string name, bool isMan, int englishScore, int mathScore) { this.id = id; this.name = name; if(isMan) { this.gender = Gender.Male; } else { this.gender = Gender.Female; } this.englishScore = englishScore; this.mathScore = mathScore; } }
LinqTest.cs
MakeStudentメソッドに名前, 性別, 英語と数学の点数を渡してStudentインスタンスをnewしています!
Unityをやっていると、MonoBehaviourを継承していないクラスをnewして使うのが新鮮ですね!
とりあえず「たろう」「じろう」「はなこ」「さとこ」のStudentが完成しました!
LINQ Sum関数
Sum関数を使えば、配列の数値を合計した値を出すことができます!
先ほどのソースのStartメソッドで説明していきます〜
void Start () { MakeStudent("たろう", true, 90, 80); MakeStudent("じろう", true, 70, 70); MakeStudent("はなこ", false, 100, 100); MakeStudent("さとこ", false, 50, 30); // Sum関数テスト Debug.Log("英語合計点(男)" + humans.Where(h => h.gender == Student.Gender.Male).Sum(h => h.englishScore)); Debug.Log("英語合計点(女)" + humans.Where(h => h.gender == Student.Gender.Female).Sum(h => h.englishScore)); }
Where関数を使い性別でフィルターをかけた後、Sum関数で合計しています!
Sum関数の引数には、ラムダ式を記述することができます!内容としてはSelect関数と同じで、englishScoreを抜き出しています!
LINQのSelect関数やWhere関数についてはこちらを参照してください!
hiyotama.hatenablog.com
男性と女性の英語の点数の合計が、無事出ました!
LINQ Average関数
続いてAverage関数です!配列の数値の平均を出します!
void Start () { MakeStudent("たろう", true, 90, 80); MakeStudent("じろう", true, 70, 70); MakeStudent("はなこ", false, 100, 100); MakeStudent("さとこ", false, 50, 30); // Average関数テスト Debug.Log("英語平均点(男):" + humans.Where(h => h.gender == Student.Gender.Male).Average(h => h.englishScore)); Debug.Log("英語平均点(女):" + humans.Where(h => h.gender == Student.Gender.Female).Average(h => h.englishScore)); }
Where関数を使い性別でフィルターをかけ、Average関数でenglishScoreの平均を出しています!
LINQ Min関数 Max関数
最後はMin関数とMax関数です!
配列の数値の最低値と最高値を出します!
void Start () { MakeStudent("たろう", true, 90, 80); MakeStudent("じろう", true, 70, 70); MakeStudent("はなこ", false, 100, 100); MakeStudent("さとこ", false, 50, 30); // Min関数テスト Debug.Log("数学最低点(男)" + humans.Where(h => h.gender == Student.Gender.Male).Min(h => h.mathScore)); Debug.Log("数学最低点(女)" + humans.Where(h => h.gender == Student.Gender.Female).Min(h => h.mathScore)); // Max関数テスト Debug.Log("数学最高点(男)" + humans.Where(h => h.gender == Student.Gender.Male).Max(h => h.mathScore)); Debug.Log("数学最高点(女)" + humans.Where(h => h.gender == Student.Gender.Female).Max(h => h.mathScore)); }
例のごとくWhere関数で性別でフィルターをかけ、Min/Max関数で結果の数値を頂いてます!
今まで配列を読む時はfor文やforeach文、その場限りのフラグ変数を作ってきましたが、
LINQを使えばだいたい1〜2行で結果を得ることができます!
バリバリ使っていこうと思います〜!