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

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

C#
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
C#
public static TSource? FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

返回

TSource

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

例外

sourcenull

示例

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

C#
// 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
*/

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

C#
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
*/

注解

方法 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) 如示例部分所述。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

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

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

C#
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
C#
public static TSource? FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);

类型参数

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>>) 。 第二个查询中,序列中没有满足条件的元素。

C#
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.
*/

注解

此方法至少有一个类型的 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的第一个元素,如果没有元素满足条件,则返回默认值。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

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

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

C#
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, TSource defaultValue);

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

defaultValue
TSource

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

返回

TSource

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

例外

sourcenull

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

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

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

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

C#
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate, TSource defaultValue);

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

predicate
Expression<Func<TSource,Boolean>>

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

defaultValue
TSource

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

返回

TSource

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

例外

sourcepredicatenull

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9