Linq-element-operators
提供:Dev Guides
LINQの要素演算子
DefaultIfEmptyを除き、残りの8つの標準クエリ要素演算子はすべて、コレクションから単一の要素を返します。
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 |
ElementAtの例-Enumerable.ElementAtメソッド
C#
VB
上記のC#またはVBのコードをコンパイルして実行すると、次の結果が生成されます-
注-ここでは、上記の出力は動的に変化し、名前はランダムに選択されます。
Firstの例-Enumerable.Firstメソッド
C#
VB
上記のC#またはVBのコードをコンパイルして実行すると、次の結果が生成されます-
Lastの例-Enumerable.Lastメソッド
C#
VB
上記のC#またはVBのコードをコンパイルして実行すると、次の結果が生成されます-