Enumerable.Single Methode
Definition
Gibt ein einzelnes spezifisches Element einer Sequenz zurückReturns a single, specific element of a sequence.
Überlädt
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
Gibt das einzige Element einer Sequenz zurück, das eine angegebene Bedingung erfüllt, und löst eine Ausnahme aus, wenn mehrere solche Elemente vorhanden sind.Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists. |
Single<TSource>(IEnumerable<TSource>) |
Gibt das einzige Element einer Sequenz zurück und löst eine Ausnahme aus, wenn nicht genau ein Element in der Sequenz vorhanden ist.Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. |
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Gibt das einzige Element einer Sequenz zurück, das eine angegebene Bedingung erfüllt, und löst eine Ausnahme aus, wenn mehrere solche Elemente vorhanden sind.Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
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
Typparameter
- TSource
Der Typ der Elemente von source
.The type of the elements of source
.
Parameter
- source
- IEnumerable<TSource>
Ein IEnumerable<T>, aus dem ein einzelnes Element zurückgegeben werden sollAn IEnumerable<T> to return a single element from.
Eine Funktion zum Überprüfen eines Elements auf eine Bedingung.A function to test an element for a condition.
Gibt zurück
- TSource
Das einzige Element der Eingabesequenz, das eine Bedingung erfüllt.The single element of the input sequence that satisfies a condition.
Ausnahmen
source
oder predicate
ist null
.source
or predicate
is null
.
Kein Element erfüllt die Bedingung in predicate
.No element satisfies the condition in predicate
.
- oder --or-
Die Bedingung in predicate
wird von mehreren Elementen erfüllt.More than one element satisfies the condition in predicate
.
- oder --or- Die Quellsequenz ist leer.The source sequence is empty.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie verwendet wird, Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) um das einzige Element eines Arrays auszuwählen, das eine Bedingung erfüllt.The following code example demonstrates how to use Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to select the only element of an array that satisfies a condition.
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}")
Im folgenden Codebeispiel wird veranschaulicht, dass Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) eine Ausnahme auslöst, wenn die Sequenz nicht genau ein Element enthält, das die Bedingung erfüllt.The following code example demonstrates that Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) throws an exception when the sequence does not contain exactly one element that satisfies the condition.
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.
Hinweise
Die- Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Methode löst eine Ausnahme aus, wenn die Eingabe Sequenz kein entsprechendes-Element enthält.The Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if the input sequence contains no matching element. Verwenden Sie stattdessen, um zurückzugeben null
, wenn kein entsprechendes Element gefunden wird SingleOrDefault .To instead return null
when no matching element is found, use SingleOrDefault.
Gilt für:
Single<TSource>(IEnumerable<TSource>)
Gibt das einzige Element einer Sequenz zurück und löst eine Ausnahme aus, wenn nicht genau ein Element in der Sequenz vorhanden ist.Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
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
Typparameter
- TSource
Der Typ der Elemente von source
.The type of the elements of source
.
Parameter
- source
- IEnumerable<TSource>
Ein IEnumerable<T>, dessen einziges Element zurückgegeben werden sollAn IEnumerable<T> to return the single element of.
Gibt zurück
- TSource
Das einzige Element der Eingabesequenz.The single element of the input sequence.
Ausnahmen
source
ist null
.source
is null
.
Die Eingabesequenz enthält mehr als ein Element.The input sequence contains more than one element.
- oder --or-
Die Eingabesequenz ist leer.The input sequence is empty.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie verwendet wird, Single<TSource>(IEnumerable<TSource>) um das einzige Element eines Arrays auszuwählen.The following code example demonstrates how to use Single<TSource>(IEnumerable<TSource>) to select the only element of an array.
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}")
Im folgenden Codebeispiel wird veranschaulicht, dass Single<TSource>(IEnumerable<TSource>) eine Ausnahme auslöst, wenn die Sequenz nicht genau ein Element enthält.The following code example demonstrates that Single<TSource>(IEnumerable<TSource>) throws an exception when the sequence does not contain exactly one element.
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.
Hinweise
Die- Single<TSource>(IEnumerable<TSource>) Methode löst eine Ausnahme aus, wenn die Eingabe Sequenz leer ist.The Single<TSource>(IEnumerable<TSource>) method throws an exception if the input sequence is empty. Um stattdessen zurückzugeben null
, wenn die Eingabe Sequenz leer ist, verwenden Sie SingleOrDefault .To instead return null
when the input sequence is empty, use SingleOrDefault.