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#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Operators {
class Program {
static void Main(string[] args) {
string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
Random random = new Random(DateTime.Now.Millisecond);
string name = names.ElementAt(random.Next(0, names.Length));
Console.WriteLine("The name chosen at random is '{0}'.", name);
Console.ReadLine();
}
}
}
VB
Module Module1
Sub Main()
Dim names() As String = _{"Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu"}
Dim random As Random = New Random(DateTime.Now.Millisecond)
Dim name As String = names.ElementAt(random.Next(0, names.Length))
MsgBox("The name chosen at random is " & name)
End Sub
End Module
上記のC#またはVBのコードをコンパイルして実行すると、次の結果が生成されます-
The name chosen at random is Ito, Shu
注-ここでは、上記の出力は動的に変化し、名前はランダムに選択されます。
Firstの例-Enumerable.Firstメソッド
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Operators {
class Program {
static void Main(string[] args) {
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19 };
int first = numbers.First();
Console.WriteLine(first);
Console.ReadLine();
}
}
}
VB
Module Module1
Sub Main()
Dim numbers() As Integer = _{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19}
Dim first As Integer = numbers.First()
MsgBox(first)
End Sub
End Module
上記のC#またはVBのコードをコンパイルして実行すると、次の結果が生成されます-
9
Lastの例-Enumerable.Lastメソッド
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Operators {
class Program {
static void Main(string[] args) {
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19 };
int last = numbers.Last();
Console.WriteLine(last);
Console.ReadLine();
}
}
}
VB
Module Module1
Sub Main()
Dim numbers() As Integer = _{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19};
Dim last As Integer = numbers.Last()
MsgBox(last)
End Sub
End Module
上記のC#またはVBのコードをコンパイルして実行すると、次の結果が生成されます-
19