Queryable.Single Metoda

Definicja

Zwraca pojedynczy, konkretny element sekwencji.

Przeciążenia

Single<TSource>(IQueryable<TSource>)

Zwraca jedyny element sekwencji i zgłasza wyjątek, jeśli nie ma dokładnie jednego elementu w sekwencji.

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

Zwraca jedyny element sekwencji, który spełnia określony warunek, i zgłasza wyjątek, jeśli istnieje więcej niż jeden taki element.

Single<TSource>(IQueryable<TSource>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca jedyny element sekwencji i zgłasza wyjątek, jeśli nie ma dokładnie jednego elementu w sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element do IQueryable<T> zwrócenia pojedynczego elementu.

Zwraca

TSource

Pojedynczy element sekwencji danych wejściowych.

Wyjątki

source to null.

source ma więcej niż jeden element.

-lub-

Sekwencja źródłowa jest pusta.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Single<TSource>(IQueryable<TSource>) polecenia , aby wybrać jedyny element tablicy.

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

Uwagi

Metoda Single<TSource>(IQueryable<TSource>) generuje element MethodCallExpression , który reprezentuje wywołanie Single<TSource>(IQueryable<TSource>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Single<TSource>(IQueryable<TSource>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca jedyny element w elemecie source.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca jedyny element sekwencji, który spełnia określony warunek, i zgłasza wyjątek, jeśli istnieje więcej niż jeden taki element.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element do IQueryable<T> zwrócenia pojedynczego elementu z.

predicate
Expression<Func<TSource,Boolean>>

Funkcja do testowania elementu dla warunku.

Zwraca

TSource

Pojedynczy element sekwencji danych wejściowych, który spełnia warunek w predicateelemecie .

Wyjątki

source lub predicate to null.

Żaden element nie spełnia warunku w elemecie predicate.

-lub-

Więcej niż jeden element spełnia warunek w elemecie predicate.

-lub-

Sekwencja źródłowa jest pusta.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) polecenia , aby wybrać jedyny element tablicy, który spełnia warunek.

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.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument typu jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) generuje element MethodCallExpression , który reprezentuje wywołanie Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca jedyny element, który source spełnia warunek określony przez predicate.

Dotyczy