Queryable.FirstOrDefault 方法

定义

返回序列中的第一个元素;如果未找到该元素,则返回默认值。

重载

FirstOrDefault<TSource>(IQueryable<TSource>)

返回序列中的第一个元素;如果序列中不包含任何元素,则返回默认值。

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

返回序列中满足指定条件的第一个元素,如果未找到这样的元素,则返回默认值。

FirstOrDefault<TSource>(IQueryable<TSource>, TSource)

返回序列中的第一个元素;如果序列中不包含任何元素,则返回默认值。

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

返回序列中满足条件的第一个元素;如果未找到这样的元素,则返回默认值。

FirstOrDefault<TSource>(IQueryable<TSource>)

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

返回序列中的第一个元素;如果序列中不包含任何元素,则返回默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

返回

TSource

如果 source 为空,则为 default(TSource);否则为 source 中的第一个元素。

例外

sourcenull

示例

下面的代码示例演示如何 FirstOrDefault<TSource>(IQueryable<TSource>) 在空序列上使用 。

// Create an empty array.
int[] numbers = { };
// Get the first item in the array, or else the
// default value for type int (0).
int first = numbers.AsQueryable().FirstOrDefault();

Console.WriteLine(first);

/*
    This code produces the following output:

    0
*/
' Create an empty array.
Dim numbers() As Integer = {}
' Get the first item in the array, or else the 
' default value for type int, which is 0.
Dim first As Integer = numbers.AsQueryable().FirstOrDefault()

MsgBox(first)

' This code produces the following output:

' 0

有时,如果集合不包含任何元素,则 的值 default(TSource) 不是要使用的默认值。 可以使用 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 方法指定在集合为空时要使用的默认值,而不是检查不需要的默认值的结果,然后根据需要对其进行更改。 然后,调用 First<TSource>(IQueryable<TSource>) 以获取第一个元素。 如果数值月份的集合为空,下面的代码示例使用这两种方法来获取默认值 1。 由于整数的默认值为 0,它不对应于任何月份,因此必须将默认值指定为 1。 查询完成后,检查第一个结果变量是否为不需要的默认值。 第二个结果变量是通过调用 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 将默认值指定为 1 来获取的。

List<int> months = new List<int> { };

// Setting the default value to 1 after the query.
int firstMonth1 = months.AsQueryable().FirstOrDefault();
if (firstMonth1 == 0)
{
    firstMonth1 = 1;
}
Console.WriteLine("The value of the firstMonth1 variable is {0}", firstMonth1);

// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int firstMonth2 = months.AsQueryable().DefaultIfEmpty(1).First();
Console.WriteLine("The value of the firstMonth2 variable is {0}", firstMonth2);

/*
 This code produces the following output:

 The value of the firstMonth1 variable is 1
 The value of the firstMonth2 variable is 1
*/
Dim months As New List(Of Integer)(New Integer() {})

' Setting the default value to 1 after the query.
Dim firstMonth1 As Integer = months.AsQueryable().FirstOrDefault()
If firstMonth1 = 0 Then
    firstMonth1 = 1
End If
MsgBox(String.Format("The value of the firstMonth1 variable is {0}", firstMonth1))

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim firstMonth2 As Integer = months.AsQueryable().DefaultIfEmpty(1).First()
MsgBox(String.Format("The value of the firstMonth2 variable is {0}", firstMonth2))

' This code produces the following output:
'
' The value of the firstMonth1 variable is 1
' The value of the firstMonth2 variable is 1

注解

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

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

如果 为空,方法 FirstOrDefault 不提供指定要返回 source 的默认值的方法。 如果要指定 除 以外的 default(TSource)默认值,请使用 方法, DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 如示例部分所述。

适用于

FirstOrDefault<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 FirstOrDefault(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
public static TSource? FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member FirstOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function FirstOrDefault(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 指定的测试,则为 default(TSource),否则为 source 中通过 predicate 指定的测试的第一个元素。

例外

sourcepredicatenull

示例

下面的代码示例演示如何通过传入谓词来使用 FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 。 第二个查询中,序列中没有满足条件的元素。

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

// Get the first string in the array that is longer
// than 20 characters, or the default value for type
// string (null) if none exists.
string firstLongName =
    names.AsQueryable().FirstOrDefault(name => name.Length > 20);

Console.WriteLine("The first long name is '{0}'.", firstLongName);

// Get the first string in the array that is longer
// than 30 characters, or the default value for type
// string (null) if none exists.
string firstVeryLongName =
    names.AsQueryable().FirstOrDefault(name => name.Length > 30);

Console.WriteLine(
    "There is {0} name that is longer than 30 characters.",
    string.IsNullOrEmpty(firstVeryLongName) ? "NOT a" : "a");

/*
    This code produces the following output:

    The first long name is 'Andersen, Henriette Thaulow'.
    There is NOT a name that is longer than 30 characters.
*/
Dim names() As String = {"Hartono, Tommy", "Adams, Terry", _
                     "Andersen, Henriette Thaulow", _
                     "Hedlund, Magnus", "Ito, Shu"}

' Get the first string in the array that is longer
' than 20 characters, or the default value for type
' string (null) if none exists.
Dim firstLongName As String = _
            names.AsQueryable().FirstOrDefault(Function(name) name.Length > 20)

MsgBox(String.Format("The first long name is '{0}'.", firstLongName))

' Get the first string in the array that is longer
' than 30 characters, or the default value for type
' string (null) if none exists.
Dim firstVeryLongName As String = _
    names.AsQueryable().FirstOrDefault(Function(name) name.Length > 30)

MsgBox(String.Format( _
    "There is {0} name that is longer than 30 characters.", _
    IIf(String.IsNullOrEmpty(firstVeryLongName), "NOT a", "a")))

' This code produces the following output:
'
' The first long name is 'Andersen, Henriette Thaulow'.
' There is NOT a name that is longer than 30 characters.

注解

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

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

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

适用于

FirstOrDefault<TSource>(IQueryable<TSource>, TSource)

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

返回序列中的第一个元素;如果序列中不包含任何元素,则返回默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

defaultValue
TSource

如果序列为空,则返回的默认值。

返回

TSource

defaultValue 如果 source 为空,则为 ;否则为 中的 source第一个元素。

例外

sourcenull

适用于

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

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

返回序列中满足条件的第一个元素;如果未找到这样的元素,则返回默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

predicate
Expression<Func<TSource,Boolean>>

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

defaultValue
TSource

如果序列为空,则返回的默认值。

返回

TSource

defaultValue 如果 source 为空,或者没有元素通过 指定的 predicate测试,则为 ;否则为 中 source 通过 指定的 predicate测试的第一个元素。

例外

sourcepredicatenull

适用于