Enumerable.Single 메서드

정의

시퀀스의 특정 단일 요소를 반환합니다.

오버로드

Single<TSource>(IEnumerable<TSource>)

시퀀스의 유일한 요소를 반환하고, 시퀀스에 요소가 정확히 하나 들어 있지 않으면 예외를 throw합니다.

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

시퀀스에서 지정된 조건에 맞는 유일한 요소를 반환하고, 이러한 요소가 둘 이상 있으면 예외를 throw합니다.

Single<TSource>(IEnumerable<TSource>)

시퀀스의 유일한 요소를 반환하고, 시퀀스에 요소가 정확히 하나 들어 있지 않으면 예외를 throw합니다.

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

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

단일 요소를 반환할 IEnumerable<T>입니다.

반환

TSource

입력 시퀀스의 단일 요소입니다.

예외

source이(가) null인 경우

입력 시퀀스에 요소가 두 개 이상 있습니다.

또는

입력 시퀀스가 비어 있는 경우

예제

다음 코드 예제에서는 배열의 유일한 요소를 선택 하는 데 사용 Single<TSource>(IEnumerable<TSource>) 하는 방법을 보여 줍니다.

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

다음 코드 예제에서는 시퀀스에 정확히 하나의 요소가 포함되지 않은 경우 예외를 throw하는 방법을 보여 Single<TSource>(IEnumerable<TSource>) 줍니다.

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.

설명

Single<TSource>(IEnumerable<TSource>) 입력 시퀀스가 비어 있으면 메서드가 예외를 throw합니다. 입력 시퀀스가 비어 있을 때 대신 반환 null 하려면 .를 사용합니다 SingleOrDefault.

적용 대상

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

시퀀스에서 지정된 조건에 맞는 유일한 요소를 반환하고, 이러한 요소가 둘 이상 있으면 예외를 throw합니다.

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

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

단일 요소를 반환할 IEnumerable<T>입니다.

predicate
Func<TSource,Boolean>

요소를 조건에 대해 테스트하는 함수입니다.

반환

TSource

입력 시퀀스에서 특정 조건에 맞는 단일 요소입니다.

예외

source 또는 predicatenull인 경우

predicate의 조건을 충족하는 요소가 없는 경우

또는

predicate의 조건에 맞는 요소가 둘 이상인 경우

또는

소스 시퀀스가 비어 있는 경우

예제

다음 코드 예제에서는 조건을 충족하는 Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 배열의 유일한 요소를 선택하는 방법을 보여 줍니다.

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

다음 코드 예제에서는 시퀀스에 조건을 충족하는 Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 요소가 하나만 포함되어 있지 않을 때 예외를 throw하는 방법을 보여 줍니다.

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.

설명

Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 입력 시퀀스에 일치하는 요소가 없는 경우 메서드는 예외를 throw합니다. 일치하는 요소를 찾을 수 없는 경우 대신 반환 null 하려면 .를 사용합니다 SingleOrDefault.

적용 대상