Enumerable.Select 메서드

정의

시퀀스의 각 요소를 새 폼에 투영합니다.

오버로드

Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>)

요소의 인덱스를 통합하여 시퀀스의 각 요소를 새 폼에 투영합니다.

Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)

시퀀스의 각 요소를 새 폼에 투영합니다.

Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>)

요소의 인덱스를 통합하여 시퀀스의 각 요소를 새 폼에 투영합니다.

public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ Select(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, TResult> ^ selector);
public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,TResult> selector);
static member Select : seq<'Source> * Func<'Source, int, 'Result> -> seq<'Result>
<Extension()>
Public Function Select(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer, TResult)) As IEnumerable(Of TResult)

형식 매개 변수

TSource

source 요소의 형식입니다.

TResult

selector에서 반환하는 값의 형식입니다.

매개 변수

source
IEnumerable<TSource>

변형 함수를 호출할 값 시퀀스입니다.

selector
Func<TSource,Int32,TResult>

각 소스 요소에 적용할 변형 함수이며, 이 함수의 두 번째 매개 변수는 소스 요소의 인덱스를 나타냅니다.

반환

IEnumerable<TResult>

해당 요소가 source의 각 요소에 대해 변형 함수를 호출한 결과인 IEnumerable<T>입니다.

예외

source 또는 selectornull인 경우

예제

다음 코드 예제에서는 를 사용하여 Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) 값 시퀀스를 프로젝싱하고 각 요소의 인덱스를 사용하는 방법을 보여 줍니다.

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

var query =
    fruits.Select((fruit, index) =>
                      new { index, str = fruit.Substring(0, index) });

foreach (var obj in query)
{
    Console.WriteLine("{0}", obj);
}

/*
 This code produces the following output:

 { index = 0, str =  }
 { index = 1, str = b }
 { index = 2, str = ma }
 { index = 3, str = ora }
 { index = 4, str = pass }
 { index = 5, str = grape }
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Project each item in the array to an anonymous type
' that stores the item's index in the array and
' a substring of each item whose length is equal
' to the index position in the original array.
Dim query =
fruits.Select(Function(fruit, index) _
                  New With {index, .Str = fruit.Substring(0, index)})

Dim output As New System.Text.StringBuilder
For Each obj In query
    output.AppendLine(obj.ToString())
Next

' Display the output.
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' { index = 0, Str =  }
' { index = 1, Str = b }
' { index = 2, Str = ma }
' { index = 3, Str = ora }
' { index = 4, Str = pass }
' { index = 5, Str = grape }

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

처리할 selector 요소를 나타내는 첫 번째 인수입니다. 소스 시퀀스에서 해당 요소의 인덱스(0부터 시작)를 나타내는 두 번째 인수 selector 입니다. 예를 들어 요소가 알려진 순서에 있고 특정 인덱스에서 요소를 사용하여 작업을 수행하려는 경우에 유용할 수 있습니다. 하나 이상의 요소의 인덱스도 검색하려는 경우에도 유용할 수 있습니다.

이 프로젝션 메서드에 필요 변형 함수 selector소스 시퀀스의 각 값에 대해 하나의 값을 생성 하 source합니다. 경우 selector 소비자가 수동으로 컬렉션이 것은 컬렉션 자체는 값을 반환 합니다. 이러한 상황에서 쿼리가 결합된 단일 값 시퀀스를 반환하는 것이 더 좋을 수 있습니다. 이렇게 하려면 대신 Select메서드를 SelectMany 사용합니다. 는 과 유사하게 Select작동하지만 SelectMany 변환 함수가 컬렉션이 반환되기 전에 확장된 SelectMany 컬렉션을 반환한다는 점에서 다릅니다.

적용 대상

Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)

시퀀스의 각 요소를 새 폼에 투영합니다.

public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ Select(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TResult> ^ selector);
public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
static member Select : seq<'Source> * Func<'Source, 'Result> -> seq<'Result>
<Extension()>
Public Function Select(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, TResult)) As IEnumerable(Of TResult)

형식 매개 변수

TSource

source 요소의 형식입니다.

TResult

selector에서 반환하는 값의 형식입니다.

매개 변수

source
IEnumerable<TSource>

변형 함수를 호출할 값 시퀀스입니다.

selector
Func<TSource,TResult>

각 요소에 적용할 변환 함수입니다.

반환

IEnumerable<TResult>

해당 요소가 source의 각 요소에 대해 변형 함수를 호출한 결과인 IEnumerable<T>입니다.

예외

source 또는 selectornull인 경우

예제

다음 코드 예제에서는 를 사용하여 Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) 값 시퀀스를 프로젝스하는 방법을 보여 줍니다.

IEnumerable<int> squares =
    Enumerable.Range(1, 10).Select(x => x * x);

foreach (int num in squares)
{
    Console.WriteLine(num);
}
/*
 This code produces the following output:

 1
 4
 9
 16
 25
 36
 49
 64
 81
 100
*/
' Create a collection of sequential integers
' from 1 to 10 and project their squares.
Dim squares As IEnumerable(Of Integer) =
Enumerable.Range(1, 10).Select(Function(x) x * x)

Dim output As New System.Text.StringBuilder
For Each num As Integer In squares
    output.AppendLine(num)
Next

' Display the output.
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' 1
' 4
' 9
' 16
' 25
' 36
' 49
' 64
' 81
' 100

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

이 프로젝션 메서드에 필요 변형 함수 selector소스 시퀀스의 각 값에 대해 하나의 값을 생성 하 source합니다. 경우 selector 소비자가 수동으로 컬렉션이 것은 컬렉션 자체는 값을 반환 합니다. 이러한 상황에서 쿼리가 결합된 단일 값 시퀀스를 반환하는 것이 더 좋을 수 있습니다. 이렇게 하려면 대신 Select메서드를 SelectMany 사용합니다. 는 과 유사하게 Select작동하지만 SelectMany 변환 함수가 컬렉션이 반환되기 전에 확장된 SelectMany 컬렉션을 반환한다는 점에서 다릅니다.

쿼리 식 구문 select 에서 (C#) 또는 Select (Visual Basic) 절은 의 Select호출로 변환됩니다.

추가 정보

적용 대상