🥺

【C#】LINQメソッド百科事典

11 min read

当記事はQiitaからの移行記事です。
一部情報が古くなっている可能性があります。

要素の取得(単一)

該当の要素が無い場合
  OrDefaultを付けていないメソッドは例外をスローする
  OrDefaultを付けたメソッドは型の既定値を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.ElementAtOrDefault(8);
// -> 0

ElementAt

指定した 位置(インデックス) にある要素を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.ElementAt(2);
// -> 52

First

条件に合う最初の要素を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.First(e => e > 15);
// -> 16

Last

条件に合う最後の要素を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Last(e => e > 15);
// -> 21

Single

唯一の要素を返す
該当する要素が複数ある場合例外をスローする

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Single(e => e > 50);
// -> 52

要素の取得(複数)

Where

条件を満たす要素を全て返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Where(e => e > 20);
// -> {52, 21}

Distinct

重複を除いたシーケンスを返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Distinct();
// -> {11, 16, 52, 21, 8}

Skip

先頭から指定された数の要素をスキップし、残りのシーケンスを返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Skip(3);
// -> {21, 8, 8}

SkipWhile

先頭から指定された条件を満たさなくなるまで要素をスキップし、残りのシーケンスを返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Skip(e => e < 20);
// -> {52, 21, 8, 8}

Take

先頭から指定された数の要素を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Take(3);
// -> {11, 16, 52}

TakeWhile

先頭から指定された条件を満たす要素を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.TakeWhile(e => e < 20);
// -> {11, 16}

集計

Max

最大値を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Max():
// -> 52

Min

最小値を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Min():
// -> 8

Average

平均値を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Average();
// -> 19.3333333333333

Sum

合計値を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Sum();
// -> 116

Count

要素数を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Count();
// -> 6

Aggregate

アキュムレータ関数で処理した結果を返す

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Aggregate((now, next) => now + next); //やっている事はSUMと同じ
// -> 116

判定

All

全ての要素が条件を満たしているか判定

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.All(e => e > 10);
// -> False

Any

条件を満たす要素が含まれているか判定

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Any(e => e > 10);
// -> True

Contains

指定した要素が含まれているか判定

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.Contains(20);
// -> False

SequenceEqual

2つのシーケンスが等しいかどうか判定

var source = new[] { 11, 16, 52, 21, 8, 8 };
var output = source.SequenceEqual(new[] { 11, 16, 21, 52, 8, 8});
// -> False

集合

Union

指定したシーケンスとの和集合を返す

var first = new[] { 11, 16, 52, 21, 8, 8 };
var second = new[] { 16, 21, 20, 3 };
var output = first.Union(second);
// -> {11, 16, 52, 21, 8, 20, 3}

Except

指定したシーケンスとの差集合を返す

var first = new[] { 11, 16, 52, 21, 8, 8 };
var second = new[] { 16, 21, 20, 3 };
var output = first.Except(second);
// -> {11, 52, 8}

Intersect

指定したシーケンスとの積集合を返す

var first = new[] { 11, 16, 52, 21, 8, 8 };
var second = new[] { 16, 21, 20, 3 };
var output = first.Intersect(second);
// -> {16, 21}

ソート

OrderBy

昇順にソートしたシーケンスを返す

var source = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var output = source.OrderBy(e => e.Version);
// -> {{ Name = C#, Version = 7 },
//     { Name = PHP, Version = 7 },
//     { Name = Java, Version = 9 },
//     { Name = C++, Version = 17 }}

OrderByDescending

降順にソートしたシーケンスを返す

var source = new[] {
    new{ Name = "C#", Version = 7 },
    new{ Name = "C++", Version = 17 },
    new{ Name = "Java", Version = 9 },
    new{ Name = "PHP", Version = 7 },
};
var output = source.OrderByDescending(e => e.Version);
// -> {{ Name = C++, Version = 17 },
//     { Name = Java, Version = 9 },
//     { Name = C#, Version = 7 },
//     { Name = PHP, Version = 7 }}

ThenBy

ソートしたシーケンスに対し、キーが等しい要素同士を昇順にソートしたシーケンスを返す

var source = new[] {
    new{ Name = "C#", Version = 7 },
    new{ Name = "C++", Version = 17 },
    new{ Name = "Java", Version = 9 },
    new{ Name = "PHP", Version = 7 },
};
var output = source.OrderBy(e => e.Version)
                   .ThenBy(e => e.Name.Length);
// -> {{ Name = C#, Version = 7 },
//     { Name = PHP, Version = 7 },
//     { Name = Java, Version = 9 },
//     { Name = C++, Version = 17 }}

ThenByDescending

ソートしたシーケンスに対し、キーが等しい要素同士を降順にソートしたシーケンスを返す

var source = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var output = source.OrderBy(e => e.Version)
                   .ThenByDescending(e => e.Name.Length);
// -> {{ Name = PHP, Version = 7 },
//     { Name = C#, Version = 7 },
//     { Name = Java, Version = 9 },
//     { Name = C++, Version = 17 }}

Reverse

逆順にソートしたシーケンスを返す

var source = new[] {
    new{Name = "C#", Version = 7},
    new{Name = "C++", Version = 17},
    new{Name = "Java", Version = 9},
    new{Name = "PHP", Version = 7},
};
var output = source.Reverse();
// -> {{ Name = PHP, Version = 7 },
//     { Name = Java, Version = 9 },
//     { Name = C++, Version = 17 },
//     { Name = C#, Version = 7 }}

射影

Select

1つの要素を単一の要素に射影する

var source = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var output = source.Select(e => e.Name);
// -> {{ C#, C++, Java, PHP }

SelectMany

1つの要素から複数の要素に射影し、その結果を1つのシーケンスとして返す

var source = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var output = source.SelectMany(e => e.Name);
// -> {{ C, #, C, +, +, J, a, v, a, P, H, P }

GroupBy

指定のキーで要素をグループ化し、そのキーとグループのシーケンスを返す

var source = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var output = source.GroupBy(e => e.Version);
// -> { Key=7, Source={{ Name = C#, Version = 7 }, { Name = PHP, Version = 7 }},
//      Key=17, Source={{ Name = C++, Version = 17}},
//      Key=9, Source={{ Name = Java, Version = 9}}}

結合

Join

内部結合を行ったシーケンスを返す

var outer = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var inner = new[] {
    new { Name = "C#", DesignedBy = "Microsoft" },
    new { Name = "Java", DesignedBy = "Sun Microsystems" },
    new { Name = "Java", DesignedBy = "Oracle" },
};
var output = outer.Join(inner,
                        o => o.Name,
                        i => i.Name, 
                        (o, i) => new { o.Name, o.Version, i.DesignedBy});
// -> {{ Name = C#, Version = 7, DesignedBy = Microsoft },
//     { Name = Java, Version = 9, DesignedBy = Sun Microsystems },
//     { Name = Java, Version = 9, DesignedBy = Oracle }}

GroupJoin

左部結合を行って指定のキーでグループ化し、そのキーとグループのシーケンスを返す

var outer = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var inner = new[] {
    new { Name = "C#", DesignedBy = "Microsoft" },
    new { Name = "Java", DesignedBy = "Sun Microsystems" },
    new { Name = "Java", DesignedBy = "Oracle" },
};
var output = outer.GroupJoin(inner,
                        o => o.Name,
                        i => i.Name, 
                        (o, i) => new { o.Name, o.Version, DesignedBy = i.Select(e => e.DesignedBy)});
// -> {{ Name = C#, Version = 7, DesigndBy = {Microsoft} },
//     { Name = C++, Version = 17, DesigndBy = {} },
//     { Name = Java, Version = 9, DesigndBy = {Sun Microsystems, Oracle} },
//     { Name = PHP, Version = 7, DesigndBy = {} }}

Concat

2つのシーケンスを連結する

var outer = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var outer2 = new[] {
     new { Name = "Swift", Version = 4 },
     new { Name = "Visual Basic", Version = 6 },
};
var output = outer.Concat(outer2);
// -> {{ Name = C#, Version = 7 },
//     { Name = C++, Version = 17 },
//     { Name = java, Version = 9 },
//     { Name = PHP, Version = 7 },
//     { Name = Swift, Version = 4},
//     { Name = Visual Basic, Version = 6}}

DefaultIfEmpty

シーケンスを返す
シーケンスが空の場合、既定値もしくは任意の要素を返す

var outer = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var output = outer.DefaultIfEmpty();
// -> {{ Name = C#, Version = 7 },
//     { Name = C++, Version = 17 },
//     { Name = java, Version = 9 },
//     { Name = PHP, Version = 7 }}

Zip

指定した関数で、2つのシーケンスを1つのシーケンスにマージする

var outer = new[] {
    new { Name = "C#", Version = 7 },
    new { Name = "C++", Version = 17 },
    new { Name = "Java", Version = 9 },
    new { Name = "PHP", Version = 7 },
};
var outer2 = new[] {
     new { Name = "Swift", Version = 4 },
     new { Name = "Visual Basic", Version = 6 },
};
var output = outer.Zip(outer2, (o1, o2) => o1.Name + "&" + o2.Name);
// -> { C#&Swift, C++&VisualBasic }