Queryable.Single Metodo

Definizione

Restituisce un singolo, specifico elemento di una sequenza.

Overload

Single<TSource>(IQueryable<TSource>)

Restituisce l'unico elemento di una sequenza e genera un'eccezione se nella sequenza non è presente esattamente un elemento.

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

Restituisce l'unico elemento di una sequenza che soddisfa una condizione specificata e genera un'eccezione se esistono più elementi di tale tipo.

Single<TSource>(IQueryable<TSource>)

Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs

Restituisce l'unico elemento di una sequenza e genera un'eccezione se nella sequenza non è presente esattamente un elemento.

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

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> di cui restituire il singolo elemento.

Restituisce

TSource

Singolo elemento della sequenza di input.

Eccezioni

source è null.

source presenta più di un elemento.

-oppure-

La sequenza di origine è vuota.

Esempio

Nell'esempio di codice seguente viene illustrato come usare Single<TSource>(IQueryable<TSource>) per selezionare l'unico elemento di una matrice.

// 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.

Commenti

Il Single<TSource>(IQueryable<TSource>) metodo genera un MethodCallExpression oggetto che rappresenta Single<TSource>(IQueryable<TSource>) se stesso come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Single<TSource>(IQueryable<TSource>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che restituisce l'unico elemento in source.

Si applica a

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

Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs

Restituisce l'unico elemento di una sequenza che soddisfa una condizione specificata e genera un'eccezione se esistono più elementi di tale tipo.

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

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IQueryable<TSource>

Un oggetto IQueryable<T> dal quale restituire un singolo elemento.

predicate
Expression<Func<TSource,Boolean>>

Funzione per testare un elemento per una condizione.

Restituisce

TSource

Il singolo elemento della sequenza di input che soddisfa la condizione in predicate.

Eccezioni

source o predicate è null.

Nessun elemento soddisfa la condizione in predicate.

-oppure-

Più di un elemento soddisfa la condizione in predicate.

-oppure-

La sequenza di origine è vuota.

Esempio

Nell'esempio di codice seguente viene illustrato come usare Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) per selezionare l'unico elemento di una matrice che soddisfa una condizione.

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.

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.

Il Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) metodo genera un MethodCallExpression oggetto che rappresenta Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) se stesso come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che restituisce l'unico elemento in source che soddisfa la condizione specificata da predicate.

Si applica a