Queryable.Single Metoda

Definice

Vrátí jeden konkrétní prvek sekvence.

Přetížení

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

Vrátí jediný prvek sekvence, která splňuje zadanou podmínku, a vyvolá výjimku, pokud existuje více než jeden takový prvek.

Single<TSource>(IQueryable<TSource>)

Vrátí jediný prvek sekvence a vyvolá výjimku, pokud v sekvenci není právě jeden prvek.

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí jediný prvek sekvence, která splňuje zadanou podmínku, a vyvolá výjimku, pokud existuje více než jeden takový prvek.

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

Parametry typu

TSource

Typ prvků .source

Parametry

source
IQueryable<TSource>

IQueryable<T>, ze které se má vrátit jeden prvek.

predicate
Expression<Func<TSource,Boolean>>

Funkce k otestování prvku pro podmínku.

Návraty

TSource

Jediný prvek vstupní sekvence, která splňuje podmínku v predicate.

Atributy

Výjimky

source nebo predicate je null.

Žádný prvek nesplňuje podmínku v predicate.

nebo

Podmínka v predicatesplňuje více než jeden prvek .

nebo

Zdrojová sekvence je prázdná.

Příklady

Následující příklad kódu ukazuje, jak použít Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) k výběru jediného prvku pole, který splňuje podmínku.

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.

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda a bude zkompilován do Expression<TDelegate>.

Metoda Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) vygeneruje MethodCallExpression , která představuje samotné volání Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu Execute<TResult>(Expression)IQueryProvider reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) , závisí na implementaci typu parametru source . Očekávané chování je, že vrátí jediný prvek v source, který splňuje podmínku určenou predicate.

Platí pro

Single<TSource>(IQueryable<TSource>)

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí jediný prvek sekvence a vyvolá výjimku, pokud v sekvenci není právě jeden prvek.

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

Parametry typu

TSource

Typ prvků .source

Parametry

source
IQueryable<TSource>

IQueryable<T> vrátit jeden prvek.

Návraty

TSource

Jeden prvek vstupní sekvence.

Atributy

Výjimky

source je null.

source má více než jeden prvek.

nebo

Zdrojová sekvence je prázdná.

Příklady

Následující příklad kódu ukazuje, jak použít Single<TSource>(IQueryable<TSource>) vybrat jediný prvek pole.

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

Poznámky

Metoda Single<TSource>(IQueryable<TSource>) vygeneruje MethodCallExpression , která představuje samotné volání Single<TSource>(IQueryable<TSource>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu Execute<TResult>(Expression)IQueryProvider reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání Single<TSource>(IQueryable<TSource>) , závisí na implementaci typu parametru source . Očekávané chování je, že vrací jediný prvek v source.

Platí pro