Queryable.ElementAtOrDefault Metoda

Definicja

Przeciążenia

ElementAtOrDefault<TSource>(IQueryable<TSource>, Index)

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32)

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

ElementAtOrDefault<TSource>(IQueryable<TSource>, Index)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAtOrDefault(System::Linq::IQueryable<TSource> ^ source, Index index);
public static TSource? ElementAtOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, Index index);
static member ElementAtOrDefault : System.Linq.IQueryable<'Source> * Index -> 'Source
<Extension()>
Public Function ElementAtOrDefault(Of TSource) (source As IQueryable(Of TSource), index As Index) As TSource

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element do IQueryable<T> zwrócenia elementu z.

index
Index

Indeks elementu do pobrania, który znajduje się od początku lub na końcu.

Zwraca

TSource

default jeśli index znajduje się poza granicami source sekwencji; w przeciwnym razie element w określonej pozycji w source sekwencji.

Wyjątki

source to null.

Dotyczy

ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAtOrDefault(System::Linq::IQueryable<TSource> ^ source, int index);
public static TSource ElementAtOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, int index);
public static TSource? ElementAtOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, int index);
static member ElementAtOrDefault : System.Linq.IQueryable<'Source> * int -> 'Source
<Extension()>
Public Function ElementAtOrDefault(Of TSource) (source As IQueryable(Of TSource), index As Integer) As TSource

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element do IQueryable<T> zwrócenia elementu z.

index
Int32

Indeks zerowy elementu do pobrania.

Zwraca

TSource

default(TSource) jeśli index znajduje się poza granicami source; w przeciwnym razie element w określonej pozycji w .source

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać polecenia ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32). W tym przykładzie użyto wartości index , która znajduje się poza granicami sekwencji źródłowej.

string[] names = { "Hartono, Tommy", "Adams, Terry",
                   "Andersen, Henriette Thaulow",
                   "Hedlund, Magnus", "Ito, Shu" };

int index = 20;

string name = names.AsQueryable().ElementAtOrDefault(index);

Console.WriteLine(
    "The name chosen at index {0} is '{1}'.",
    index,
    String.IsNullOrEmpty(name) ? "[NONE AT THIS INDEX]" : name);

/*
    This code produces the following output:

    The name chosen at index 20 is '[NONE AT THIS INDEX]'.
*/
Dim names() As String = {"Hartono, Tommy", "Adams, Terry", _
                   "Andersen, Henriette Thaulow", _
                   "Hedlund, Magnus", "Ito, Shu"}

Dim index As Integer = 20

Dim name As String = names.AsQueryable().ElementAtOrDefault(index)

MsgBox(String.Format("The name at index {0} is '{1}'.", _
    index, IIf(String.IsNullOrEmpty(name), "[NONE AT THIS INDEX]", name)))

' This code produces the following output:
'
' The name at index 20 is '[NONE AT THIS INDEX]'.

Uwagi

Metoda ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) generuje element MethodCallExpression , który reprezentuje wywołanie ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca element w pozycji w sourceelemencie index , lub default(TSource) jeśli index znajduje się poza granicami sourceelementu .

Dotyczy