Linq-query-operators

提供:Dev Guides
移動先:案内検索

LINQ-クエリ演算子

クエリパターンを形成する一連の拡張メソッドは、LINQ Standard Query Operatorsと呼ばれます。 LINQクエリ式の構成要素として、これらの演算子はフィルタリング、並べ替え、投影、集計などのさまざまなクエリ機能を提供します。

LINQ標準クエリ演算子は、機能に基づいて次のカテゴリに分類できます。

  • フィルタリング演算子
  • 結合演算子
  • 投影操作
  • ソート演算子
  • グループ化演算子
  • コンバージョン数
  • 連結
  • 集約
  • 量指定子の操作
  • パーティション操作
  • 発電事業
  • セット操作
  • 平等
  • 要素演算子

フィルタリング演算子

フィルタリングは、特定の条件を満たす選択された要素のみを持つように結果セットを制限する操作です。

link:/linq/linq_filtering_operators [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
where Filter values based on a predicate function where Where
OfType Filter values based on their ability to be as a specified type Not Applicable Not Applicable

結合演算子

結合とは、相互の関係を直接的な方法で追跡することが困難なデータソースを対象とする操作を指します。

link:/linq/linq_join_operators [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Join The operator join two sequences on basis of matching keys join … in … on … equals … From x In …, y In … Where x.a = y.a
GroupJoin Join two sequences and group the matching elements join … in … on … equals … into … Group Join … In … On …

投影操作

投影とは、オブジェクトを特定のプロパティのみを持つまったく新しいフォームに変換する操作です。

リンク:/linq/linq_projection_operations [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Select The operator projects values on basis of a transform function select Select
SelectMany The operator project the sequences of values which are based on a transform function as well as flattens them into a single sequence Use multiple from clauses Use multiple From clauses

ソート演算子

ソート操作を使用すると、単一または複数の属性に基づいてシーケンスの要素を並べることができます。

link:/linq/linq_sorting_operators [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
OrderBy The operator sort values in an ascending order orderby Order By
OrderByDescending The operator sort values in a descending order orderby …​ descending Order By …​ Descending
ThenBy Executes a secondary sorting in an ascending order orderby …, … Order By …, …
ThenByDescending Executes a secondary sorting in a descending order orderby …, … descending Order By …, … Descending
Reverse Performs a reversal of the order of the elements in a collection Not Applicable Not Applicable

グループ化演算子

オペレーターは、共通の共有属性に基づいていくつかのグループにデータを入れます。

link:/linq/linq_grouping_operators [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
GroupBy Organize a sequence of items in groups and return them as an IEnumerable collection of type IGrouping<key, element> group … by -or- group … by … into … Group … By … Into …
ToLookup Execute a grouping operation in which a sequence of key pairs are returned Not Applicable Not Applicable

コンバージョン数

演算子は入力オブジェクトのタイプを変更し、さまざまなアプリケーションで使用されます。

link:/linq/linq_conversions [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
AsEnumerable Returns the input typed as IEnumerable<T> Not Applicable Not Applicable
AsQueryable A (generic) IEnumerable is converted to a (generic) IQueryable Not Applicable Not Applicable
Cast Performs casting of elements of a collection to a specified type Use an explicitly typed range variable. Eg:from string str in words From … As …
OfType Filters values on basis of their , depending on their capability to be cast to a particular type Not Applicable Not Applicable
ToArray Forces query execution and does conversion of a collection to an array Not Applicable Not Applicable
ToDictionary On basis of a key selector function set elements into a Dictionary<TKey, TValue> and forces execution of a LINQ query Not Applicable Not Applicable
ToList Forces execution of a query by converting a collection to a List<T> Not Applicable Not Applicable
ToLookup Forces execution of a query and put elements into a Lookup<TKey, TElement> on basis of a key selector function Not Applicable Not Applicable

連結

2つのシーケンスの連結を実行します。これは、重複を削除しないという事実を除いて、操作に関してはUnion演算子に非常に似ています。

link:/linq/linq_concatenation [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Concat Two sequences are concatenated for the formation of a single one sequence. Not Applicable Not Applicable

集約

任意のタイプの必要な集計を実行し、LINQでカスタム集計を作成できます。

link:/linq/linq_aggregation [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Aggregate Operates on the values of a collection to perform custom aggregation operation Not Applicable Not Applicable
Average Average value of a collection of values is calculated Not Applicable Aggregate … In … Into Average()
Count Counts the elements satisfying a predicate function within collection Not Applicable Aggregate … In … Into Count()
LonCount Counts the elements satisfying a predicate function within a huge collection Not Applicable Aggregate … In … Into LongCount()
Max Find out the maximum value within a collection Not Applicable Aggregate … In … Into Max()
Min Find out the minimum value existing within a collection Not Applicable Aggregate … In … Into Min()
Sum Find out the sum of a values within a collection Not Applicable Aggregate … In … Into Sum()

量指定子の操作

これらの演算子はブール値、つまり シーケンス内の一部またはすべての要素が特定の条件を満たす場合に真または偽。

link:/linq/linq_quantifier_operations [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
All Returns a value ‘True’ if all elements of a sequence satisfy a predicate condition Not Applicable Aggregate … In … Into All(…)
Any Determines by searching a sequence that whether any element of the same satisfy a specified condition Not Applicable Aggregate … In … Into Any()
Contains Returns a ‘True’ value if finds that a specific element is there in a sequence if the sequence doe not contains that specific element , ‘false’ value is returned Not Applicable Not Applicable

パーティション演算子

入力シーケンスを2つのセクションに分割します。シーケンスの要素を並べ替えてから1つを返すことはありません。

link:/linq/linq_partition_operators [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Skip Skips some specified number of elements within a sequence and returns the remaining ones Not Applicable Skip
SkipWhile Same as that of Skip with the only exception that number of elements to skip are specified by a Boolean condition Not Applicable Skip While
Take Take a specified number of elements from a sequence and skip the remaining ones Not Applicable Take
TakeWhile Same as that of Take except the fact that number of elements to take are specified by a Boolean condition Not Applicable Take While

発電事業

世代演算子によって新しい値のシーケンスが作成されます。

リンク:/linq/linq_generation_operations [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
DefaultIfEmpty When applied to an empty sequence, generate a default element within a sequence Not Applicable Not Applicable
Empty Returns an empty sequence of values and is the most simplest generational operator Not Applicable Not Applicable
Range Generates a collection having a sequence of integers or numbers Not Applicable Not Applicable
Repeat Generates a sequence containing repeated values of a specific length Not Applicable Not Applicable

セット操作

集合演算には4つの演算子があり、それぞれ異なる基準に基づいて結果を生成します。

link:/linq/linq_set_operations [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Distinct Results a list of unique values from a collection by filtering duplicate data if any Not Applicable Distinct
Except Compares the values of two collections and return the ones from one collection who are not in the other collection Not Applicable Not Applicable
Intersect Returns the set of values found t be identical in two separate collections Not Applicable Not Applicable
Union Combines content of two different collections into a single list that too without any duplicate content Not Applicable Not Applicable

平等

2つの文(列挙可能)を比較し、それらが完全に一致するかどうかを判断します。

link:/linq/linq_equality [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
SequenceEqual Results a Boolean value if two sequences are found to be identical to each other Not Applicable Not Applicable

要素演算子

DefaultIfEmptyを除き、残りの8つの標準クエリ要素演算子はすべて、コレクションから単一の要素を返します。

link:/linq/linq_element_operators [例を表示]

Operator Description C# Query Expression Syntax VB Query Expression Syntax
ElementAt Returns an element present within a specific index in a collection Not Applicable Not Applicable
ElementAtOrDefault Same as ElementAt except of the fact that it also returns a default value in case the specific index is out of range Not Applicable Not Applicable
First Retrieves the first element within a collection or the first element satisfying a specific condition Not Applicable Not Applicable
FirstOrDefault Same as First except the fact that it also returns a default value in case there is no existence of such elements Not Applicable Not Applicable
Last Retrieves the last element present in a collection or the last element satisfying a specific condition Not Applicable Not Applicable
LastOrDefault Same as Last except the fact that it also returns a default value in case there is no existence of any such element Not Applicable Not Applicable
Single Returns the lone element of a collection or the lone element that satisfy a certain condition Not Applicable Not Applicable
SingleOrDefault Same as Single except that it also returns a default value if there is no existence of any such lone element Not Applicable Not Applicable
DefaultIfEmpty Returns a default value if the collection or list is empty or null Not Applicable Not Applicable