LINQ - 拡張メソッド一覧

提供:MochiuWiki : SUSE, EC, PCB
2021年11月21日 (日) 07:09時点におけるWiki (トーク | 投稿記録)による版 (文字列「source lang」を「syntaxhighlight lang」に置換)
ナビゲーションに移動 検索に移動

概要

C#のLINQを種類ごとにまとめて、簡単なサンプルを記述する。

ここでは、結果の表示において、独自の拡張メソッドToResult(this IEnumerable)を使用している。
このメソッドを以下に記述する。

<syntaxhighlight lang="c#">
// 結果表示用の拡張メソッド
public static String ToResult<TSource>(this IEnumerable<TSource> source)
{
   return "{" + string.Join(", ", source) + "}";
}

public static String ToResult<TKey, TSource>(this IEnumerable<IGrouping<TKey, TSource>> source)
{
   return source.Select(group => string.Format("Key={0}, Source={1}", group.Key, group.ToResult())).ToResult();
}
</source>



要素の取得(単一)

該当の要素がない場合

  • ~OrDefaultが付いていないメソッドは例外をスローする。
  • ~OrDefaultが付いたメソッドは型の規定値を返す。
メソッド名 機能
ElementAt
ElementAtOrDefault
指定した位置(インデックス)にある要素を返す。
First
FirstOrDefault
最初の要素を返す。
Last
LastOrDefault
最後の要素を返す。
Single
SingleOrDefault
唯一の要素を返す。該当する要素が複数ある場合、例外をスローする。
<syntaxhighlight lang="c#">
var source = new[] { 3, 4, 5, 6, 7, 8, 9, 9 };

Console.WriteLine(source.ElementAt(2));
// → 5

Console.WriteLine(source.ElementAtOrDefault(10));
// → 0

Console.WriteLine(source.First());
// → 3

Console.WriteLine(source.First(e => e > 5));
// → 6

Console.WriteLine(source.Last());
// → 9

Console.WriteLine(source.Last(e => e < 5));
// → 4

Console.WriteLine(source.Single());
// → System.InvalidOperationException: シーケンスに複数の要素が含まれている

Console.WriteLine(source.Single(e => e < 4 ));
// → 3
</source>



要素の取得(複数)

メソッド名 機能
Where 条件を満たす要素をすべて返す。
Distinct 重複を除いたシーケンスを返す。
Skip 先頭から指定された数の要素をスキップし、残りのシーケンスを返す。
SkipWhile 先頭から指定された条件を満たさなくなるまで要素をスキップし、残りのシーケンスを返す。
Take 先頭から指定された数の要素を返す。
TakeWhile 先頭から指定された条件を満たす要素を返す。
<syntaxhighlight lang="c#">
var source = new[] { 3, 4, 5, 6, 7, 8, 9, 9 };

Console.WriteLine(source.Where(e => e > 5).ToResult());
// → {6, 7, 8, 9, 9}

Console.WriteLine(source.Distinct().ToResult());
// → {3, 4, 5, 6, 7, 8, 9}

Console.WriteLine(source.Skip(5).ToResult());
// → {8, 9, 9}

Console.WriteLine(source.SkipWhile(e => e < 5).ToResult());
// → {5, 6, 7, 8, 9, 9}

Console.WriteLine(source.Take(5).ToResult());
// → {3, 4, 5, 6, 7}

Console.WriteLine(source.TakeWhile(e => e < 5).ToResult());
// → {3, 4}
</source>



集計

メソッド名 機能
Max 最大値を返す。
Min 最小値を返す。
Average 平均値を返す。
Sum 合計を返す。
Count 要素数を返す。
Aggregate アキュムレータ関数で処理した結果を返す。
<syntaxhighlight lang="c#">
var source = new[] { 3, 4, 5, 6, 7, 8, 9, 9 };

Console.WriteLine(source.Max());
// → 9

Console.WriteLine(source.Min());
// → 3

Console.WriteLine(source.Average());
// → 6.375

Console.WriteLine(source.Sum());
// → 51

Console.WriteLine(source.Count());
// → 8

Console.WriteLine(source.Aggregate((now, next) => now * next));
// → 1632960

// 参考:標本分散
double ave = source.Average();
Console.WriteLine(source.Sum(e => Math.Pow(e - ave, 2)) / source.Count());
// → 4.484375
</source>



ソート

メソッド名 機能
OrderBy 昇順にソートしたシーケンスを返す。
OrderByDescending 降順にソートしたシーケンスを返す。
ThenBy ソートしたシーケンスに対し、キーが等しい要素同士を昇順にソートしたシーケンスを返す。
ThenByDescending ソートしたシーケンスに対し、キーが等しい要素同士を降順にソートしたシーケンスを返す。
Reverse 逆順にソートしたシーケンスを返す。
<syntaxhighlight lang="c#">
var source = new[] {
   new{Name = "C#", Age = 11},
   new{Name = "Java", Age = 16},
   new{Name = "Groovy", Age = 8},
   new{Name = "Scala", Age = 8},
};

Console.WriteLine(source.OrderBy(e => e.Age).ToResult());
// → {{ Name = Groovy, Age = 8 },
//     { Name = Scala, Age = 8 }, 
//     { Name = C#, Age = 11 },
//     { Name = Java, Age = 16 }}

Console.WriteLine(source.OrderByDescending(e => e.Age).ToResult());
// → {{ Name = Java, Age = 16 },
//     { Name = C#, Age = 11 },
//     { Name = Groovy, Age = 8 },
//     { Name = Scala, Age = 8 }}

Console.WriteLine(source.OrderBy(e => e.Age).ThenBy(e => e.Name.Length).ToResult());
// → {{ Name = Scala, Age = 8 },
//     { Name = Groovy, Age = 8 },
//     { Name = C#, Age = 11 },
//     { Name = Java, Age = 16 }}

Console.WriteLine(source.OrderBy(e => e.Age).ThenByDescending(e => e.Name.Length).ToResult());
// → {{ Name = Groovy, Age = 8 },
//     { Name = Scala, Age = 8 },
//     { Name = C#, Age = 11 },
//     { Name = Java, Age = 16 }}

Console.WriteLine(source.Reverse().ToResult());
// → {{ Name = Scala, Age = 8 },
//     { Name = Groovy, Age = 8 },
//     { Name = Java, Age = 16},
//     { Name = C#, Age = 11 }}
</source>



射影

メソッド名 機能
Select 1つの要素を単一の要素に射影する。
SelectMany 1つの要素から複数の要素に射影する。その結果を1つのシーケンスとして返す。
GroupBy 指定のキーで要素をグループ化する。その"キーとグループ" のシーケンスを返す。
<syntaxhighlight lang="c#">
var source = new[] {
   new{Name = "C#", Age = 11},
   new{Name = "Java", Age = 16},
   new{Name = "Groovy", Age = 8},
   new{Name = "Scala", Age = 8},
};

Console.WriteLine(source.Select(e => e.Name).ToResult());
// → {C#, Java, Groovy, Scala}

Console.WriteLine(source.SelectMany(e => e.Name.ToCharArray()).ToResult());
// → {C, #, J, a, v, a, G, r, o, o, v, y, S, c, a, l, a}

Console.WriteLine(source.GroupBy(e => e.Age).ToResult());
// → {Key=11, Source=テンプレート:Name = C,
//     Key=16, Source=テンプレート:Name = Java, Age = 16,
//     Key=8, Source={{ Name = Groovy, Age = 8 }, { Name = Scala, Age = 8 }}}
</source>



結合

メソッド名 機能
Join 内部結合を行ったシーケンスを返す。
GroupJoin 左外部結合を行って指定のキーでグループ化する。その "キーとグループ" のシーケンスを返す。
Concat 2つのシーケンスを連結する。(Unionは同じ要素を一つにまとめるが、Concatは元の要素をすべて返す)
DefaultIfEmpty シーケンスを返す。シーケンスが空なら、規定値もしくは任意の要素を返す。
Zip 指定した関数で、2つのシーケンスを1つのシーケンスにマージする。
<syntaxhighlight lang="c#">
var outer = new[] {
   new{Name = "C#", Age = 11},
   new{Name = "Java", Age = 16},
   new{Name = "Groovy", Age = 8},
   new{Name = "Scala", Age = 8},
};
var outer2 = new[] {
   new{Name = "Python", Age = 21},
   new{Name = "COBOL", Age = 52},
};
var inner = new[] {
   new{Name = "C#", DesignedBy = "Microsoft"},
   new{Name = "Java", DesignedBy = "Sun Microsystems"},
   new{Name = "Java", DesignedBy = "Oracle"},
};

Console.WriteLine(outer.Join(inner, o => o.Name, i => i.Name, (o, i) => new { o.Name, o.Age, i.DesignedBy}).ToResult());
// → テンプレート:Name = C

Console.WriteLine(outer.GroupJoin(inner, o => o.Name, i => i.Name, (o, i) =>
                                  new { o.Name, o.Age, DesigndBy = i.Select(e => e.DesignedBy).ToResult() }).ToResult());
// → テンプレート:Name = C

Console.WriteLine(outer.Concat(outer2).ToResult());
// → テンプレート:Name = C

Console.WriteLine(outer.DefaultIfEmpty().ToResult());
// → テンプレート:Name = C

Console.WriteLine(outer.Zip(outer2, (o1, o2) => o1.Name + "&" + o2.Name).ToResult());
// → {C#&Python, Java&COBOL}
</source>



変換

メソッド名 機能
OfType 各要素を指定した型に変換する。
キャストできない要素は除外する。
Cast 各要素を指定した型に変換する。
キャストできない要素が含まれていた場合、例外をスローする。
ToArray 配列を作成する。
ToDictionary 連想配列(ディクショナリ)を作成する。
ToList リストを作成する。
ToLookup キーコレクション*1を生成する。
IEnumerableと同じ名前のメソッドがクラス内に定義されている場合に使用する。
そのままだと、クラス内のメソッドが優先的に選択されて、
IEnumerableの拡張メソッドが呼びだせないためである。
AsEnumerable IEnumerableを返す。
1対多のディクショナリである。
例えば、〜.ToLookup()["hoge"]と実行すると、hogeに紐付く要素の集合(IEnumerable)が返る。


<syntaxhighlight lang="c#">
ArrayList mixed = new ArrayList { "C#", "Java", 3.141592653, "Groovy", "Scala" };

Console.WriteLine(mixed.OfType<string>().ToResult());
// → {C#, Java, Groovy, Scala}

Console.WriteLine(mixed.Cast<string>().ToResult());
// → System.InvalidCastException: 
//    型 'System.Double' のオブジェクトを型 'System.String' にキャストできません。
</source>