Queryable.Single 方法

定義

傳回序列的單一特定項目。

多載

Single<TSource>(IQueryable<TSource>)

傳回序列的唯一一個元素,如果序列中不是正好一個元素,則擲回例外狀況。

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

傳回序列中符合指定之條件的唯一一個元素,如果有一個以上這類元素,則擲回例外狀況。

Single<TSource>(IQueryable<TSource>)

傳回序列的唯一一個元素,如果序列中不是正好一個元素,則擲回例外狀況。

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>>)

傳回序列中符合指定之條件的唯一一個元素,如果有一個以上這類元素,則擲回例外狀況。

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 指定條件的專案。

適用於