Queryable.Single 메서드

정의

시퀀스의 특정 단일 요소를 반환합니다.Returns a single, specific element of a sequence.

오버로드

Single<TSource>(IQueryable<TSource>)

시퀀스의 유일한 요소를 반환하고, 시퀀스에 요소가 정확히 하나 들어 있지 않으면 예외를 throw합니다.Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

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

시퀀스에서 지정된 조건에 맞는 유일한 요소를 반환하고, 이러한 요소가 둘 이상 있으면 예외를 throw합니다.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>(IQueryable<TSource>)

시퀀스의 유일한 요소를 반환하고, 시퀀스에 요소가 정확히 하나 들어 있지 않으면 예외를 throw합니다.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::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 요소의 형식입니다.The type of the elements of source.

매개 변수

source
IQueryable<TSource>

단일 요소를 반환할 IQueryable<T>입니다.An IQueryable<T> to return the single element of.

반환

TSource

입력 시퀀스의 단일 요소입니다.The single element of the input sequence.

예외

source이(가) null인 경우source is null.

source에 둘 이상의 요소가 있는 경우source has more than one element.

예제

다음 코드 예제에서는를 사용 하 여 배열의 유일한 요소를 선택 하는 방법을 보여 줍니다 Single<TSource>(IQueryable<TSource>) .The following code example demonstrates how to use Single<TSource>(IQueryable<TSource>) to select the only element of an array.

// 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>) .The Single<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Single<TSource>(IQueryable<TSource>) itself as a constructed generic method. 그런 다음 MethodCallExpression Execute<TResult>(Expression) IQueryProvider Provider 매개 변수의 속성이 나타내는의 메서드에를 전달 합니다 source .It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

호출을 나타내는 식 트리를 실행 한 결과로 발생 하는 쿼리 동작은 Single<TSource>(IQueryable<TSource>) 매개 변수의 형식 구현에 따라 달라 집니다 source .The query behavior that occurs as a result of executing an expression tree that represents calling Single<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter. 예상 되는 동작은의 유일한 요소를 반환 하는 것입니다 source .The expected behavior is that it returns the only element in source.

적용 대상

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

시퀀스에서 지정된 조건에 맞는 유일한 요소를 반환하고, 이러한 요소가 둘 이상 있으면 예외를 throw합니다.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::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 요소의 형식입니다.The type of the elements of source.

매개 변수

source
IQueryable<TSource>

단일 요소를 반환할 IQueryable<T>입니다.An IQueryable<T> to return a single element from.

predicate
Expression<Func<TSource,Boolean>>

요소를 조건에 대해 테스트하는 함수입니다.A function to test an element for a condition.

반환

TSource

입력 시퀀스에서 predicate의 조건에 맞는 단일 요소입니다.The single element of the input sequence that satisfies the condition in predicate.

예외

source 또는 predicatenull인 경우source or predicate is null.

predicate의 조건을 충족하는 요소가 없는 경우No element satisfies the condition in predicate.

또는-or- predicate의 조건에 맞는 요소가 둘 이상인 경우More than one element satisfies the condition in predicate.

또는-or- 소스 시퀀스가 비어 있는 경우The source sequence is empty.

예제

다음 코드 예제에서는를 사용 하 여 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 조건에 맞는 배열의 유일한 요소를 선택 하는 방법을 보여 줍니다.The following code example demonstrates how to use Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) to select the only element of an array that satisfies a condition.

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> .This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 이러한 매개 변수는 람다 식을 전달할 수 있으며로 컴파일됩니다 Expression<TDelegate> .For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)메서드는 자신을 MethodCallExpression 생성 된 제네릭 메서드로 호출 하는을 생성 합니다 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) .The Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a MethodCallExpression that represents calling Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) itself as a constructed generic method. 그런 다음 MethodCallExpression Execute<TResult>(Expression) IQueryProvider Provider 매개 변수의 속성이 나타내는의 메서드에를 전달 합니다 source .It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

호출을 나타내는 식 트리를 실행 한 결과로 발생 하는 쿼리 동작은 Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 매개 변수의 형식 구현에 따라 달라 집니다 source .The query behavior that occurs as a result of executing an expression tree that represents calling Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the source parameter. 예상 되는 동작은에서 source 로 지정 된 조건을 충족 하는의 유일한 요소를 반환 한다는 것입니다 predicate .The expected behavior is that it returns the only element in source that satisfies the condition specified by predicate.

적용 대상