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

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource SingleOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
static member SingleOrDefault : seq<'Source> -> 'Source
<Extension()>
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource)) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

返回

TSource

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

例外

sourcenull

输入序列包含多个元素。

示例

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

string[] fruits1 = { "orange" };

string fruit1 = fruits1.SingleOrDefault();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/
' Create an array that contains one item.
Dim fruits1() As String = {"orange"}

' Get the single item in the array or else a default value.
Dim result As String = fruits1.SingleOrDefault()

' Display the result.
Console.WriteLine($"First array: {result}")

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

string[] fruits2 = { };

string fruit2 = fruits2.SingleOrDefault();

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

/*
 This code produces the following output:

 No such string!
*/
' Create an empty array.
Dim fruits2() As String = {}

result = String.Empty

' Get the single item in the array or else a default value.
result = fruits2.SingleOrDefault()

' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(result), "No single item found", result)
Console.WriteLine($"Second array: {output}")

' This code produces the following output:
'
' First array: orange
' Second array: No single item found

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

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
*/
Dim pageNumbers() As Integer = {}

' Setting the default value to 1 after the query.
Dim pageNumber1 As Integer = pageNumbers.SingleOrDefault()
If pageNumber1 = 0 Then
    pageNumber1 = 1
End If
Console.WriteLine($"The value of the pageNumber1 variable is {pageNumber1}")

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim pageNumber2 As Integer = pageNumbers.DefaultIfEmpty(1).Single()
Console.WriteLine($"The value of the pageNumber2 variable is {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) 如示例部分所述。

适用于

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

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

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource SingleOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member SingleOrDefault : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

返回

TSource

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

例外

sourcepredicatenull

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

示例

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

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
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Get the single item in the array whose length is > 10.
Dim fruit1 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 10)

' Display the result.
Console.WriteLine($"First array: {fruit1}")

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

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!
*/
' Get the single item in the array whose length is > 15.
Dim fruit2 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 15)

' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(fruit2), "No single item found", fruit2)
Console.WriteLine($"Second array: {output}")

' This code produces the following output:
'
' First array: passionfruit
' Second array: No single item found

注解

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

适用于

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

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

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

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

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

defaultValue
TSource

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

返回

TSource

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

例外

sourcenull

输入序列包含多个元素。

适用于

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

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

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource SingleOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate, TSource defaultValue);
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue);
static member SingleOrDefault : seq<'Source> * Func<'Source, bool> * 'Source -> 'Source
<Extension()>
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean), defaultValue As TSource) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

defaultValue
TSource

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

返回

TSource

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

例外

sourcepredicatenull

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

适用于