通过


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
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource Single<TSource>(this System.Linq.IQueryable<TSource> source);
static member Single : System.Linq.IQueryable<'Source> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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>

一个返回 /> 的单个元素。

返回

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

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

适用于

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

Source:
Queryable.cs
Source:
Queryable.cs
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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>>)生成一个表示调用Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)自身为已构造泛型方法的一MethodCallExpression个方法。 然后,它将传递给MethodCallExpressionExecute<TResult>(Expression)由参数属性source表示Provider的方法IQueryProvider

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

适用于