Enumerable.Single Metoda

Definicja

Zwraca pojedynczy, określony element sekwencji.

Przeciążenia

Single<TSource>(IEnumerable<TSource>, 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>(IEnumerable<TSource>)

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

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

Źródło:
Single.cs
Źródło:
Single.cs
Źródło:
Single.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::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 elementów elementu source.

Parametry

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

Funkcja do testowania elementu dla warunku.

Zwraca

TSource

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

Wyjątki

source lub predicate ma wartość 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>(IEnumerable<TSource>, Func<TSource,Boolean>) polecenia , aby wybrać jedyny element tablicy, który spełnia warunek.

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}")

Poniższy przykład kodu pokazuje, że Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) zgłasza wyjątek, gdy sekwencja nie zawiera dokładnie jednego elementu, który spełnia warunek.

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.

Uwagi

Metoda Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) zgłasza wyjątek, jeśli sekwencja wejściowa nie zawiera pasującego elementu. Aby zamiast tego powrócić null po znalezieniu pasującego elementu, użyj polecenia SingleOrDefault.

Dotyczy

Single<TSource>(IEnumerable<TSource>)

Źródło:
Single.cs
Źródło:
Single.cs
Źródło:
Single.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::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 elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> umożliwiający zwrócenie pojedynczego elementu elementu .

Zwraca

TSource

Pojedynczy element sekwencji danych wejściowych.

Wyjątki

source to null.

Sekwencja wejściowa zawiera więcej niż jeden element.

-lub-

Sekwencja wejściowa jest pusta.

Przykłady

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

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}")

Poniższy przykład kodu pokazuje, że Single<TSource>(IEnumerable<TSource>) zgłasza wyjątek, gdy sekwencja nie zawiera dokładnie jednego elementu.

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.

Uwagi

Metoda Single<TSource>(IEnumerable<TSource>) zgłasza wyjątek, jeśli sekwencja wejściowa jest pusta. Aby zamiast tego zwrócić wartość null , gdy sekwencja wejściowa jest pusta, użyj polecenia SingleOrDefault.

Dotyczy