Queryable.Single 메서드

정의

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

오버로드

Single<TSource>(IQueryable<TSource>)

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

Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

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

Single<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Single(System::Linq::IQueryable<TSource> ^ source);
public static TSource Single<TSource> (this System.Linq.IQueryable<TSource> source);
static member Single : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IQueryable(Of TSource)) As TSource

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IQueryable<TSource>

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

반환

TSource

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

예외

source이(가) null인 경우

source에 둘 이상의 요소가 있는 경우

또는

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

예제

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

// Create two arrays.
string[] fruits1 = { "orange" };
string[] fruits2 = { "orange", "apple" };

// Get the only item in the first array.
string fruit1 = fruits1.AsQueryable().Single();

Console.WriteLine("First query: " + fruit1);

try
{
    // Try to get the only item in the second array.
    string fruit2 = fruits2.AsQueryable().Single();
    Console.WriteLine("Second query: " + fruit2);
}
catch (System.InvalidOperationException)
{
    Console.WriteLine(
        "Second query: The collection does not contain exactly one element."
        );
}

/*
    This code produces the following output:

    First query: orange
    Second query: The collection does not contain exactly one element
*/
' Create two arrays.
Dim fruits1() As String = {"orange"}
Dim fruits2() As String = {"orange", "apple"}

' Get the only item in the first array.
Dim result As String = fruits1.AsQueryable().Single()

' Display the result.
MsgBox("First query: " & result)

Try
    ' Try to get the only item in the second array.
    Dim fruit2 As String = fruits2.AsQueryable().Single()
    MsgBox("Second query: " + fruit2)
Catch
    MsgBox("Second query: The collection does not contain exactly one element.")
End Try

' This code produces the following output:

' First query: orange
' Second query: The collection does not contain exactly one element.

설명

메서드는 Single<TSource>(IQueryable<TSource>)MethodCallExpression 생성된 제네릭 메서드로 자신을 호출 Single<TSource>(IQueryable<TSource>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 Execute<TResult>(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 Single<TSource>(IQueryable<TSource>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상 동작은 에서 유일한 요소를 반환한다는 것입니다 source.

적용 대상

Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Single(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource Single<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Single : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IQueryable<TSource>

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

predicate
Expression<Func<TSource,Boolean>>

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

반환

TSource

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

예외

source 또는 predicatenull인 경우

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

또는

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

또는

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

예제

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

string[] fruits = { "apple", "banana", "mango",
                      "orange", "passionfruit", "grape" };

// Get the only string in the array whose length is greater than 10.
string fruit1 = fruits.AsQueryable().Single(fruit => fruit.Length > 10);

Console.WriteLine("First Query: " + fruit1);

try
{
    // Try to get the only string in the array
    // whose length is greater than 15.
    string fruit2 = fruits.AsQueryable().Single(fruit => fruit.Length > 15);
    Console.WriteLine("Second Query: " + fruit2);
}
catch (System.InvalidOperationException)
{
    Console.Write("Second Query: The collection does not contain ");
    Console.WriteLine("exactly one element whose length is greater than 15.");
}

/*
    This code produces the following output:

    First Query: passionfruit
    Second Query: The collection does not contain exactly one
    element whose length is greater than 15.
 */
Dim fruits() As String = _
    {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Get the only string in the array whose length is greater than 10.
Dim result As String = _
    fruits.AsQueryable().Single(Function(fruit) fruit.Length > 10)

' Display the result.
MsgBox("First Query: " & result)

Try
    ' Try to get the only string in the array
    ' whose length is greater than 15.
    Dim fruit2 As String = fruits.AsQueryable().Single(Function(fruit) fruit.Length > 15)
    MsgBox("Second Query: " + fruit2)
Catch
    Dim text As String = "Second Query: The collection does not contain "
    text = text & "exactly one element whose length is greater than 15."
    MsgBox(text)
End Try

' This code produces the following output:

' First Query: passionfruit
' Second Query: The collection does not contain exactly one 
'   element whose length is greater than 15.

설명

이 메서드에는 형식 인수가 형식 중 하나인 형식 Expression<TDelegate> 의 매개 변수가 Func<T,TResult> 하나 이상 있습니다. 이러한 매개 변수의 경우 람다 식을 전달할 수 있으며 에 컴파일됩니다 Expression<TDelegate>.

메서드는 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)MethodCallExpression 생성된 제네릭 메서드로 자신을 호출 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 Execute<TResult>(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상 동작은 에서 지정된 predicate조건을 충족하는 유일한 요소를 source 반환한다는 것입니다.

적용 대상