Enumerable.Single Enumerable.Single Enumerable.Single Enumerable.Single Method

Définition

Retourne un élément spécifique unique d'une séquence.Returns a single, specific element of a sequence.

Surcharges

Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Retourne le seul élément d’une séquence qui satisfait à une condition spécifique ou lève une exception si cette séquence contient plusieurs éléments respectant cette condition.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>) Single<TSource>(IEnumerable<TSource>) Single<TSource>(IEnumerable<TSource>) Single<TSource>(IEnumerable<TSource>)

Retourne l’élément unique d’une séquence ou lève une exception si cette séquence ne contient pas un seul élément.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>) Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Retourne le seul élément d’une séquence qui satisfait à une condition spécifique ou lève une exception si cette séquence contient plusieurs éléments respectant cette condition.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

Paramètres de type

TSource

Le type des éléments de source.The type of the elements of source.

Paramètres

source
IEnumerable<TSource>

IEnumerable<T> duquel retourner un seul élément.An IEnumerable<T> to return a single element from.

predicate
Func<TSource,Boolean>

Fonction permettant de tester un élément pour une condition.A function to test an element for a condition.

Retours

TSource TSource TSource TSource

Seul élément de la séquence d'entrée à satisfaire à une condition.The single element of the input sequence that satisfies a condition.

Exceptions

source ou predicate est null.source or predicate is null.

Aucun élément ne satisfait la condition dans predicate.No element satisfies the condition in predicate.

ou-or- Plusieurs éléments satisfont à la condition dans predicate.More than one element satisfies the condition in predicate.

ou-or- La séquence source est vide.The source sequence is empty.

Exemples

L’exemple de code suivant montre comment utiliser Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) pour sélectionner le seul élément d’un tableau qui satisfait à une condition.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.
MsgBox("First query: " & result)

L’exemple de code suivant montre Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) que lève une exception lorsque la séquence ne contient pas exactement un élément qui satisfait à la condition.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.
MsgBox("Second query: " & result)

' This code produces the following output:
'
' First query: passionfruit
' Second query: There is not EXACTLY ONE element whose length is > 15.

Remarques

La Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) méthode lève une exception si la séquence d’entrée ne contient aucun élément correspondant.The Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if the input sequence contains no matching element. Pour retourner null à la place quand aucun élément correspondant n’est SingleOrDefaulttrouvé, utilisez.To instead return null when no matching element is found, use SingleOrDefault.

Single<TSource>(IEnumerable<TSource>) Single<TSource>(IEnumerable<TSource>) Single<TSource>(IEnumerable<TSource>) Single<TSource>(IEnumerable<TSource>)

Retourne l’élément unique d’une séquence ou lève une exception si cette séquence ne contient pas un seul élément.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

Paramètres de type

TSource

Le type des éléments de source.The type of the elements of source.

Paramètres

source
IEnumerable<TSource>

IEnumerable<T> duquel retourner le seul élément.An IEnumerable<T> to return the single element of.

Retours

TSource TSource TSource TSource

Seul élément de la séquence d'entrée.The single element of the input sequence.

Exceptions

La séquence d'entrée contient plusieurs éléments.The input sequence contains more than one element.

ou-or-

La séquence d'entrée est vide.The input sequence is empty.

Exemples

L’exemple de code suivant montre comment utiliser Single<TSource>(IEnumerable<TSource>) pour sélectionner le seul élément d’un tableau.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.
MsgBox("First query: " & result)

L’exemple de code suivant montre Single<TSource>(IEnumerable<TSource>) que lève une exception lorsque la séquence ne contient pas exactement un élément.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.
MsgBox("Second query: " & result)

' This code produces the following output:
'
' First query: orange
' Second query: The collection does not contain exactly one element.

Remarques

La Single<TSource>(IEnumerable<TSource>) méthode lève une exception si la séquence d’entrée est vide.The Single<TSource>(IEnumerable<TSource>) method throws an exception if the input sequence is empty. Pour retourner null à la place lorsque la séquence d’entrée est SingleOrDefaultvide, utilisez.To instead return null when the input sequence is empty, use SingleOrDefault.

S’applique à