Enumerable.SingleOrDefault 方法

定义

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

重载

SingleOrDefault<TSource>(IEnumerable<TSource>)

返回序列中的唯一元素;如果该序列为空,则返回默认值;如果该序列包含多个元素,此方法将引发异常。

SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

返回序列中满足指定条件的唯一元素;如果这类元素不存在,则返回默认值;如果有多个元素满足该条件,此方法将引发异常。

SingleOrDefault<TSource>(IEnumerable<TSource>, TSource)

返回序列的唯一元素,如果序列为空,则返回指定的默认值;如果序列中有多个元素,此方法将引发异常。

SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

返回序列中满足指定条件的唯一元素;如果没有此类元素,则返回指定的默认值;如果多个元素满足条件,此方法将引发异常。

SingleOrDefault<TSource>(IEnumerable<TSource>)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

返回序列中的唯一元素;如果该序列为空,则返回默认值;如果该序列包含多个元素,此方法将引发异常。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

返回

TSource

输入序列中的单个元素;如果序列不包含任何元素,则为 default(TSource)。

例外

sourcenull

输入序列包含多个元素。

示例

下面的代码示例演示如何使用 SingleOrDefault<TSource>(IEnumerable<TSource>) 选择数组的唯一元素。

C#
string[] fruits1 = { "orange" };

string fruit1 = fruits1.SingleOrDefault();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/

下面的代码示例演示 在 SingleOrDefault<TSource>(IEnumerable<TSource>) 序列为空时返回默认值。

C#
string[] fruits2 = { };

string fruit2 = fruits2.SingleOrDefault();

Console.WriteLine(
    String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);

/*
 This code produces the following output:

 No such string!
*/

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

C#
int[] pageNumbers = { };

// Setting the default value to 1 after the query.
int pageNumber1 = pageNumbers.SingleOrDefault();
if (pageNumber1 == 0)
{
    pageNumber1 = 1;
}
Console.WriteLine("The value of the pageNumber1 variable is {0}", pageNumber1);

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

/*
 This code produces the following output:

 The value of the pageNumber1 variable is 1
 The value of the pageNumber2 variable is 1
*/

注解

引用和可为空类型的默认值为 null

方法 SingleOrDefault 不提供指定默认值的方法。 如果要指定 除 以外的 default(TSource)默认值, DefaultIfEmpty<TSource>(IEnumerable<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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

返回序列中满足指定条件的唯一元素;如果这类元素不存在,则返回默认值;如果有多个元素满足该条件,此方法将引发异常。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
C#
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

返回

TSource

输入序列中满足条件的单个元素;如果未找到这类元素,则为 default (TSource)。

例外

sourcepredicatenull

多个元素满足 predicate 中的条件。

示例

下面的代码示例演示如何使用 SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 选择满足条件的数组的唯一元素。

C#
string[] fruits = { "apple", "banana", "mango",
                      "orange", "passionfruit", "grape" };

string fruit1 = fruits.SingleOrDefault(fruit => fruit.Length > 10);

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 passionfruit
*/

下面的代码示例演示当 SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 序列不包含满足条件的元素时返回默认值。

C#
string fruit2 =
    fruits.SingleOrDefault(fruit => fruit.Length > 15);

Console.WriteLine(
    String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);

/*
 This code produces the following output:

 No such string!
*/

注解

引用和可为空类型的默认值为 null

适用于

.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SingleOrDefault<TSource>(IEnumerable<TSource>, TSource)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

返回序列的唯一元素,如果序列为空,则返回指定的默认值;如果序列中有多个元素,此方法将引发异常。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

defaultValue
TSource

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

返回

TSource

输入序列的单个元素;如果序列不包含任何元素, defaultValue 则为 。

例外

sourcenull

输入序列包含多个元素。

适用于

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

SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

返回序列中满足指定条件的唯一元素;如果没有此类元素,则返回指定的默认值;如果多个元素满足条件,此方法将引发异常。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

defaultValue
TSource

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

返回

TSource

满足条件的输入序列中的单个元素;如果未找到此类元素, defaultValue 则为 。

例外

sourcepredicatenull

多个元素满足 predicate 中的条件。

适用于

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