共用方式為


Queryable.Single 方法

定義

回傳序列中單一且特定的元素。

多載

名稱 Description
Single<TSource>(IQueryable<TSource>)

傳回序列的唯一專案,如果序列中沒有一個專案,則會擲回例外狀況。

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

傳回序列中唯一符合指定條件的專案,如果有多個這類專案存在,則會擲回例外狀況。

Single<TSource>(IQueryable<TSource>)

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
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>

一個 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)IQueryProvider由參數Provider性質source所表示的方法。

執行代表呼叫 Single<TSource>(IQueryable<TSource>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是回傳中 source唯一的元素。

適用於

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
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>>) 方法產生的 是 MethodCallExpression ,代表 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpressionExecute<TResult>(Expression)IQueryProvider由參數Provider性質source所表示的方法。

執行代表呼叫 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是回傳中唯一符合條件predicate的元素source

適用於