Queryable.First 方法

定义

返回序列中的第一个元素。

重载

First<TSource>(IQueryable<TSource>)

返回序列中的第一个元素。

First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

返回序列中满足指定条件的第一个元素。

First<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

返回序列中的第一个元素。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要返回其第一个元素的 IQueryable<T>

返回

TSource

source 中的第一个元素。

例外

sourcenull

源序列为空。

示例

下面的代码示例演示如何使用 First<TSource>(IQueryable<TSource>) 返回序列中的第一个元素。

int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
                    83, 23, 87, 435, 67, 12, 19 };

int first = numbers.AsQueryable().First();

Console.WriteLine(first);

/*
    This code produces the following output:

    9
*/
Dim numbers() As Integer = {9, 34, 65, 92, 87, 435, 3, 54, _
                    83, 23, 87, 435, 67, 12, 19}

Dim first As Integer = numbers.AsQueryable().First()

MsgBox(first)

' This code produces the following output:
'
' 9

注解

方法 First<TSource>(IQueryable<TSource>) 生成一个 , MethodCallExpression 它将调用 First<TSource>(IQueryable<TSource>) 自身表示为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 First<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期行为是返回 中的 source第一个元素。

适用于

First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

返回序列中满足指定条件的第一个元素。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource First(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource First<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member First : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function First(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要从中返回元素的 IQueryable<T>

predicate
Expression<Func<TSource,Boolean>>

用于测试每个元素是否满足条件的函数。

返回

TSource

通过 source 中测试的 predicate 中的第一个元素。

例外

sourcepredicatenull

元素均不满足 predicate 中的条件。

- 或 -

源序列为空。

示例

下面的代码示例演示如何使用 First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 返回满足条件的序列的第一个元素。

int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
                  83, 23, 87, 435, 67, 12, 19 };

// Get the first number in the array that is greater than 80.
int first = numbers.AsQueryable().First(number => number > 80);

Console.WriteLine(first);

/*
    This code produces the following output:

    92
*/
Dim numbers() As Integer = {9, 34, 65, 92, 87, 435, 3, 54, _
                  83, 23, 87, 435, 67, 12, 19}

' Get the first number in the array that is greater than 80.
Dim first As Integer = numbers.AsQueryable().First(Function(number) number > 80)

MsgBox(first)

' This code produces the following output:
'
' 92

注解

此方法至少有一个类型 Expression<TDelegate> 参数,其类型参数是类型之 Func<T,TResult> 一。 对于这些参数,可以传入 lambda 表达式,它将编译为 Expression<TDelegate>

方法 First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 生成一个 , MethodCallExpression 它将调用 First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 自身表示为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期行为是,它返回 中 source 满足 指定 predicate条件的第一个元素。

适用于