C# Linq
C#のLinqとはListの操作をSQLライクに書けるようにしたもの。
メソッドの書き方とキーワードを使う書き方の二つがあるが、今回はメソッドを使ったやり方を書く。
まずは準備としてクラスを用意する。
class Student { public Student(int Id, string Name, int ScoreJapanese, int ScoreMath, int ScoreEnglish, int ClassNumber) { this.Id = Id; this.Name = Name; this.ScoreJapanese = ScoreJapanese; this.ScoreMath = ScoreMath; this.ScoreEnglish = ScoreEnglish; this.ClassNumber = ClassNumber; } public int Id { get; set; } public string Name { get; set; } public int ScoreJapanese { get; set; } public int ScoreMath { get; set; } public int ScoreEnglish { get; set; } public int ClassNumber { get; set; } }
次にリストに入れるデータの用意。
最後の一行がリンクを使うための準備である。
static void main(string[] args) { var Students = new List<Student>(); //ID,名前,国語の点数,数学の点数,英語の点数,クラス Students.Add(new Student(1, "taro", 92, 83, 81, 1)); Students.Add(new Student(2, "hanako", 85, 38, 97, 1)); Students.Add(new Student(3, "ryo", 92, 91, 95, 1)); Students.Add(new Student(4, "ichiro", 61, 72, 45, 2)); Students.Add(new Student(5, "runa", 52, 91, 38, 2)); Students.Add(new Student(6, "akira", 82, 85, 77, 2)); Students.Add(new Student(7, "yuka", 97, 24, 51, 3)); Students.Add(new Student(8, "yoshiki", 51, 92, 73, 3)); Students.Add(new Student(9, "arisa", 62, 73, 75, 3)); IEnumerable<Student> Linq = Students; }
Selectメソッド。
mainメソッドの最後に追加
実行すると、*名前*のように表示される。
foreach (var student in Linq.Select(x => "*" + x.Name + "*")) { Console.WriteLine(student); }
Whereメソッド
mainメソッドの最後に追加
実行するとIDが偶数のものが表示される。
foreach (var student in Linq.Where(x => x.Id % 2 == 0)) { Console.WriteLine(student.Name); }
上記2つを組み合わせたWhereメソッドとSelectメソッド
mainメソッドの最後に追加。
実行するとIDが偶数のものが*名前*の形式で表示される。
foreach (var student in Linq.Where(x => x.Id % 2 == 0).Select(x => "*" + x.Name + "*")) { Console.WriteLine(student); }
OrderByメソッド
mainメソッドの最後に追加
実行すると順番が指定したものの昇順に並びかわる。
foreach (var student in Linq.OrderBy(x => x.ScoreJapanese)) { Console.WriteLine(student.Name + ":" + student.ScoreJapanese); }
OrderByDescendingメソッド
mainメソッドの最後に追加
実行すると順番が指定したものの降順に並びかわる。
foreach (var student in Linq.OrderByDescending(x => x.ScoreJapanese)) { Console.WriteLine(student.Name + ":" + student.ScoreJapanese); }
ThenByメソッド
並び替えを2回行う場合にはOrderByを2回使うのではなく2回目はThenByを使う。
mainメソッドの最後に追加。
実行すると順番がOrderByDescendingで指定したものの降順かつ
ThenByDescendingで指定したものの降順で並びかわる。
foreach (var student in Linq.OrderByDescending(x => x.ClassNumber).ThenByDescending(x => x.ScoreEnglish)) { Console.WriteLine(student.Name + ":" + student.ClassNumber + ":" + student.ScoreEnglish); }
最後に全体を表示。
using System; using System.Collections.Generic; using System.Linq; namespace TestApp1 { class Program { static void Main(string[] args) { Console.WriteLine("start"); var Students = new List<Student>(); Students.Add(new Student(1, "taro", 92, 83, 81, 1)); Students.Add(new Student(2, "hanako", 85, 38, 97, 1)); Students.Add(new Student(3, "ryo", 92, 91, 95, 1)); Students.Add(new Student(4, "ichiro", 61, 72, 45, 2)); Students.Add(new Student(5, "runa", 52, 91, 38, 2)); Students.Add(new Student(6, "akira", 82, 85, 77, 2)); Students.Add(new Student(7, "yuka", 97, 24, 51, 3)); Students.Add(new Student(8, "yoshiki", 51, 92, 73, 3)); Students.Add(new Student(9, "arisa", 62, 73, 75, 3)); IEnumerable<Student> Linq = Students; //select foreach (var student in Linq.Select(x => "*" + x.Name + "*")) { Console.WriteLine(student); } //where foreach (var student in Linq.Where(x => x.Id % 2 == 0)) { Console.WriteLine(student.Name); } //whereとselect foreach (var student in Linq.Where(x => x.Id % 2 == 0).Select(x => "*" + x.Name + "*")) { Console.WriteLine(student); } //orderby foreach (var student in Linq.OrderBy(x => x.ScoreJapanese)) { Console.WriteLine(student.Name + ":" + student.ScoreJapanese); } //orderbyDescending foreach (var student in Linq.OrderByDescending(x => x.ScoreJapanese)) { Console.WriteLine(student.Name + ":" + student.ScoreJapanese); } //ThenByDecending foreach (var student in Linq.OrderByDescending(x => x.ClassNumber).ThenByDescending(x => x.ScoreEnglish)) { Console.WriteLine(student.Name + ":" + student.ClassNumber + ":" + student.ScoreEnglish); } } } class Student { public Student(int Id, string Name, int ScoreJapanese, int ScoreMath, int ScoreEnglish, int ClassNumber) { this.Id = Id; this.Name = Name; this.ScoreJapanese = ScoreJapanese; this.ScoreMath = ScoreMath; this.ScoreEnglish = ScoreEnglish; this.ClassNumber = ClassNumber; } public int Id { get; set; } public string Name { get; set; } public int ScoreJapanese { get; set; } public int ScoreMath { get; set; } public int ScoreEnglish { get; set; } public int ClassNumber { get; set; } } }