Enumerable.Select 메서드

정의

시퀀스의 각 요소를 새 형식으로 투영합니다.

오버로드

Name Description
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>)

Source:
Select.cs
Source:
Select.cs
Source:
Select.cs
Source:
Select.cs
Source:
Select.cs

요소의 인덱스를 통합하여 시퀀스의 각 요소를 새 형식으로 투영합니다.

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>

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

예외

source 또는 selector .입니다 null.

예제

다음 코드 예제에서는 값 시퀀스를 통해 프로젝싱하고 각 요소의 인덱스를 사용하는 방법을 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#의 foreach 또는 Visual Basic For Each 사용하여 개체가 열거될 때까지 실행되지 않습니다.

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

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

적용 대상

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

Source:
Select.cs
Source:
Select.cs
Source:
Select.cs
Source:
Select.cs
Source:
Select.cs

시퀀스의 각 요소를 새 형식으로 투영합니다.

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>

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

예외

source 또는 selector .입니다 null.

예제

다음 코드 예제에서는 값 시퀀스를 통해 프로젝트를 사용 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#의 foreach 또는 Visual Basic For Each 사용하여 개체가 열거될 때까지 실행되지 않습니다.

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

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

추가 정보

적용 대상