Enumerable.Single Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Vrátí jeden konkrétní prvek sekvence.
Přetížení
| Name | Description |
|---|---|
| Single<TSource>(IEnumerable<TSource>, 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>(IEnumerable<TSource>) |
Vrátí jediný prvek sekvence a vyvolá výjimku, pokud v sekvenci není právě jeden prvek. |
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
- Zdroj:
- Single.cs
- Zdroj:
- Single.cs
- Zdroj:
- Single.cs
- Zdroj:
- Single.cs
- Zdroj:
- Single.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::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource Single<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member Single : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As TSource
Parametry typu
- TSource
Typ prvků .source
Parametry
- source
- IEnumerable<TSource>
IEnumerable<T>, ze které se má vrátit jeden prvek.
Návraty
Jediný prvek vstupní sekvence, která splňuje podmínku.
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>(IEnumerable<TSource>, Func<TSource,Boolean>) k výběru jediného prvku pole, který splňuje podmínku.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
string fruit1 = fruits.Single(fruit => fruit.Length > 10);
Console.WriteLine(fruit1);
/*
This code produces the following output:
passionfruit
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
' Get the single item in the array whose length is greater than 10.
Dim result As String =
fruits.Single(Function(fruit) fruit.Length > 10)
' Display the result.
Console.WriteLine($"First query: {result}")
Následující příklad kódu ukazuje, že Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) vyvolá výjimku, pokud sekvence neobsahuje přesně jeden prvek, který splňuje podmínku.
string fruit2 = null;
try
{
fruit2 = fruits.Single(fruit => fruit.Length > 15);
}
catch (System.InvalidOperationException)
{
Console.WriteLine(@"The collection does not contain exactly
one element whose length is greater than 15.");
}
Console.WriteLine(fruit2);
// This code produces the following output:
//
// The collection does not contain exactly
// one element whose length is greater than 15.
result = String.Empty
' Try to get the single item in the array whose length is > 15.
Try
result = fruits.Single(Function(fruit) _
fruit.Length > 15)
Catch ex As System.InvalidOperationException
result = "There is not EXACTLY ONE element whose length is > 15."
End Try
' Display the result.
Console.WriteLine($"Second query: {result}")
' This code produces the following output:
'
' First query: passionfruit
' Second query: There is not EXACTLY ONE element whose length is > 15.
Poznámky
Metoda Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) vyvolá výjimku, pokud vstupní sekvence neobsahuje žádný odpovídající prvek. Chcete-li místo toho vrátit null , pokud nebyl nalezen žádný odpovídající prvek, použijte SingleOrDefault.
Platí pro
Single<TSource>(IEnumerable<TSource>)
- Zdroj:
- Single.cs
- Zdroj:
- Single.cs
- Zdroj:
- Single.cs
- Zdroj:
- Single.cs
- Zdroj:
- Single.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::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource Single<TSource>(this System.Collections.Generic.IEnumerable<TSource> source);
static member Single : seq<'Source> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IEnumerable(Of TSource)) As TSource
Parametry typu
- TSource
Typ prvků .source
Parametry
- source
- IEnumerable<TSource>
IEnumerable<T> vrátit jeden prvek.
Návraty
Jeden prvek vstupní sekvence.
Výjimky
source je null.
Vstupní sekvence obsahuje více než jeden prvek.
nebo
Vstupní sekvence je prázdná.
Příklady
Následující příklad kódu ukazuje, jak použít Single<TSource>(IEnumerable<TSource>) vybrat jediný prvek pole.
string[] fruits1 = { "orange" };
string fruit1 = fruits1.Single();
Console.WriteLine(fruit1);
/*
This code produces the following output:
orange
*/
' Create an array that contains one item.
Dim fruits1() As String = {"orange"}
' Get the single item in the array.
Dim result As String = fruits1.Single()
' Display the result.
Console.WriteLine($"First query: {result}")
Následující příklad kódu ukazuje, že Single<TSource>(IEnumerable<TSource>) vyvolá výjimku, když sekvence neobsahuje přesně jeden prvek.
string[] fruits2 = { "orange", "apple" };
string fruit2 = null;
try
{
fruit2 = fruits2.Single();
}
catch (System.InvalidOperationException)
{
Console.WriteLine("The collection does not contain exactly one element.");
}
Console.WriteLine(fruit2);
/*
This code produces the following output:
The collection does not contain exactly one element.
*/
' Create an array that contains two items.
Dim fruits2() As String = {"orange", "apple"}
result = String.Empty
' Try to get the 'single' item in the array.
Try
result = fruits2.Single()
Catch ex As System.InvalidOperationException
result = "The collection does not contain exactly one element."
End Try
' Display the result.
Console.WriteLine($"Second query: {result}")
' This code produces the following output:
'
' First query: orange
' Second query: The collection does not contain exactly one element.
Poznámky
Metoda Single<TSource>(IEnumerable<TSource>) vyvolá výjimku, pokud je vstupní sekvence prázdná. Pokud chcete místo toho vrátit null , když je vstupní sekvence prázdná, použijte SingleOrDefault.