Queryable.ElementAtOrDefault Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
ElementAtOrDefault<TSource>(IQueryable<TSource>, Index) |
Returns the element at a specified index in a sequence or a default value if the index is out of range. |
ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) |
Returns the element at a specified index in a sequence or a default value if the index is out of range. |
ElementAtOrDefault<TSource>(IQueryable<TSource>, Index)
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
Returns the element at a specified index in a sequence or a default value if the index is out of range.
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
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IQueryable<TSource>
An IQueryable<T> to return an element from.
- index
- Index
The index of the element to retrieve, which is either from the start or the end.
Returns
default
if index
is outside the bounds of the source
sequence; otherwise, the element at the specified position in the source
sequence.
Exceptions
source
is null
.
Applies to
ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32)
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
Returns the element at a specified index in a sequence or a default value if the index is out of range.
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
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IQueryable<TSource>
An IQueryable<T> to return an element from.
- index
- Int32
The zero-based index of the element to retrieve.
Returns
default
(TSource
) if index
is outside the bounds of source
; otherwise, the element at the specified position in source
.
Exceptions
source
is null
.
Examples
The following code example demonstrates how to use ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32). This example uses a value for index
that is outside the bounds of the source sequence.
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]'.
Remarks
The ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) depends on the implementation of the type of the source
parameter. The expected behavior is that it returns the item at position index
in source
, or default(TSource)
if index
is outside the bounds of source
.