Queryable.Single 方法

定义

返回序列中的单个特定元素。

重载

Single<TSource>(IQueryable<TSource>)

返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。

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

返回序列中满足指定条件的唯一元素;如果有多个这样的元素存在,则会引发异常。

Single<TSource>(IQueryable<TSource>)

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

返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

返回

TSource

输入序列的单个元素。

例外

sourcenull

source 具有多个元素。

- 或 -

源序列为空。

示例

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

// Create two arrays.
string[] fruits1 = { "orange" };
string[] fruits2 = { "orange", "apple" };

// Get the only item in the first array.
string fruit1 = fruits1.AsQueryable().Single();

Console.WriteLine("First query: " + fruit1);

try
{
    // Try to get the only item in the second array.
    string fruit2 = fruits2.AsQueryable().Single();
    Console.WriteLine("Second query: " + fruit2);
}
catch (System.InvalidOperationException)
{
    Console.WriteLine(
        "Second query: The collection does not contain exactly one element."
        );
}

/*
    This code produces the following output:

    First query: orange
    Second query: The collection does not contain exactly one element
*/
' Create two arrays.
Dim fruits1() As String = {"orange"}
Dim fruits2() As String = {"orange", "apple"}

' Get the only item in the first array.
Dim result As String = fruits1.AsQueryable().Single()

' Display the result.
MsgBox("First query: " & result)

Try
    ' Try to get the only item in the second array.
    Dim fruit2 As String = fruits2.AsQueryable().Single()
    MsgBox("Second query: " + fruit2)
Catch
    MsgBox("Second query: The collection does not contain exactly one element.")
End Try

' This code produces the following output:

' First query: orange
' Second query: The collection does not contain exactly one element.

注解

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

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

适用于

Single<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 Single(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource Single<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Single : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function Single(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

满足 predicate 中条件的输入序列中的单个元素。

例外

sourcepredicatenull

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

- 或 -

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

- 或 -

源序列为空。

示例

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

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

// Get the only string in the array whose length is greater than 10.
string fruit1 = fruits.AsQueryable().Single(fruit => fruit.Length > 10);

Console.WriteLine("First Query: " + fruit1);

try
{
    // Try to get the only string in the array
    // whose length is greater than 15.
    string fruit2 = fruits.AsQueryable().Single(fruit => fruit.Length > 15);
    Console.WriteLine("Second Query: " + fruit2);
}
catch (System.InvalidOperationException)
{
    Console.Write("Second Query: The collection does not contain ");
    Console.WriteLine("exactly one element whose length is greater than 15.");
}

/*
    This code produces the following output:

    First Query: passionfruit
    Second Query: The collection does not contain exactly one
    element whose length is greater than 15.
 */
Dim fruits() As String = _
    {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Get the only string in the array whose length is greater than 10.
Dim result As String = _
    fruits.AsQueryable().Single(Function(fruit) fruit.Length > 10)

' Display the result.
MsgBox("First Query: " & result)

Try
    ' Try to get the only string in the array
    ' whose length is greater than 15.
    Dim fruit2 As String = fruits.AsQueryable().Single(Function(fruit) fruit.Length > 15)
    MsgBox("Second Query: " + fruit2)
Catch
    Dim text As String = "Second Query: The collection does not contain "
    text = text & "exactly one element whose length is greater than 15."
    MsgBox(text)
End Try

' This code produces the following output:

' First Query: passionfruit
' Second Query: The collection does not contain exactly one 
'   element whose length is greater than 15.

注解

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

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

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

适用于