Help us understand the problem. What is going on with this article?

# 要素の取得(単一)

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

```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 }
```
モダンな技術とPerfumeが好きなエンジニアです。 C#, PHP, Symfony, Laravel, Javascript, TypeScript, Vue.js, React, Swift, Kotlin, Python

https://www.illmatics.co.jp/
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away