Queryable.ElementAtOrDefault Metódus

Definíció

Túlterhelések

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

Egy adott index elemet ad vissza egy sorozatban, vagy egy alapértelmezett értéket, ha az index túllépi a tartományt.

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

Egy adott index elemet ad vissza egy sorozatban, vagy egy alapértelmezett értéket, ha az index túllépi a tartományt.

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

Forrás:
Queryable.cs
Forrás:
Queryable.cs
Forrás:
Queryable.cs
Forrás:
Queryable.cs
Forrás:
Queryable.cs

Egy adott index elemet ad vissza egy sorozatban, vagy egy alapértelmezett értéket, ha az index túllépi a tartományt.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource? ElementAtOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, Index index);
static member ElementAtOrDefault : System.Linq.IQueryable<'Source> * Index -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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

Típusparaméterek

TSource

A . elemeinek sourcetípusa.

Paraméterek

source
IQueryable<TSource>

Egy IQueryable<T> elem visszaadásához.

index
Index

A lekérni kívánt elem indexe, amely az elejétől vagy végétől számít.

Válaszok

TSource

default ha index a sorozat határain source kívül esik; ellenkező esetben a sorozatban megadott pozícióban lévő source elem.

Attribútumok

Kivételek

source az null.

A következőre érvényes:

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

Forrás:
Queryable.cs
Forrás:
Queryable.cs
Forrás:
Queryable.cs
Forrás:
Queryable.cs
Forrás:
Queryable.cs

Egy adott index elemet ad vissza egy sorozatban, vagy egy alapértelmezett értéket, ha az index túllépi a tartományt.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource? ElementAtOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, int index);
static member ElementAtOrDefault : System.Linq.IQueryable<'Source> * int -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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

Típusparaméterek

TSource

A . elemeinek sourcetípusa.

Paraméterek

source
IQueryable<TSource>

Egy IQueryable<T> elem visszaadásához.

index
Int32

A lekérni kívánt elem nulla alapú indexe.

Válaszok

TSource

default(TSource) ha index kívül esik a határánsource; ellenkező esetben a megadott pozícióban lévő elem.source

Attribútumok

Kivételek

source az null.

Példák

Az alábbi példakód bemutatja a használat módját ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32). Ez a példa olyan értéket index használ, amely kívül esik a forrásütemezés határain.

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]'.

Megjegyzések

A ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) metódus létrehoz egy olyan metódust MethodCallExpression , amely önmagát generált általános metódusként jeleníti ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) meg. Ezután átadja a MethodCallExpression paraméter tulajdonsága által Execute<TResult>(Expression) képviselt metódusnak IQueryProviderProvider.source

A meghívást ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) jelképező kifejezésfa végrehajtása során előforduló lekérdezési viselkedés a paraméter típusának source implementálásától függ. A várt viselkedés az, hogy az elemet a pozícióban indexsourceadja vissza, vagy default(TSource) ha index a határán sourcekívül esik.

A következőre érvényes: