Enumerable.Select 方法

定義

將序列的每個元素規劃成一個新的表單。Projects each element of a sequence into a new form.

多載

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

透過加入項目的索引,將序列的每個項目投影成新的表單。Projects each element of a sequence into a new form by incorporating the element's index.

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

將序列的每個元素規劃成一個新的表單。Projects each element of a sequence into a new form.

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

透過加入項目的索引,將序列的每個項目投影成新的表單。Projects each element of a sequence into a new form by incorporating the element's index.

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 項目的類型。The type of the elements of source.

TResult

selector 所傳回值的型別。The type of the value returned by selector.

參數

source
IEnumerable<TSource>

要對於叫用轉換函式的值序列。A sequence of values to invoke a transform function on.

selector
Func<TSource,Int32,TResult>

要套用到每個來源項目的轉換函式;此函式的第二個參數代表來源項目的索引。A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

傳回

IEnumerable<TResult>

IEnumerable<T>,其項目是對 source 之各個項目叫用轉換函式所產生的結果。An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.

例外狀況

sourceselectornullsource or selector is null.

範例

下列程式碼範例示範如何使用 Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) 來投射值序列,並使用每個專案的索引。The following code example demonstrates how to use Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) to project over a sequence of values and use the index of each element.

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 }

備註

這個方法是使用延後執行所執行。This method is implemented by using deferred execution. 立即傳回值是一個物件,它會儲存執行動作所需的所有資訊。The immediate return value is an object that stores all the information that is required to perform the action. 這個方法所表示的查詢不會執行,直到以 GetEnumerator foreach Visual c # 或 Visual Basic 中的方法來列舉物件為止 For EachThe query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

第一個引數, selector 表示要處理的元素。The first argument to selector represents the element to process. 第二個引數, selector 代表來源序列中該專案以零為起始的索引。The second argument to selector represents the zero-based index of that element in the source sequence. 例如,如果專案是已知順序,而您想要使用特定索引的專案來進行某個動作,這會很有用。This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. 如果您想要取得一或多個專案的索引,這也會很有用。It can also be useful if you want to retrieve the index of one or more elements.

此投射方法需要轉換函數, selector 以針對來源序列中的每個值產生一個值 sourceThis projection method requires the transform function, selector, to produce one value for each value in the source sequence, source. 如果傳回 selector 本身為集合的值,則會由取用者以手動方式進行個子序列。If selector returns a value that is itself a collection, it is up to the consumer to traverse the subsequences manually. 在這種情況下,您的查詢可能更適合傳回單一的結合值序列。In such a situation, it might be better for your query to return a single coalesced sequence of values. 若要達成此目的,請使用 SelectMany 方法,而不是 SelectTo achieve this, use the SelectMany method instead of Select. 雖然 SelectMany 的運作方式類似,但轉換函式會 Select 傳回集合,然後在傳回之後展開 SelectManyAlthough SelectMany works similarly to Select, it differs in that the transform function returns a collection that is then expanded by SelectMany before it is returned.

適用於

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

將序列的每個元素規劃成一個新的表單。Projects each element of a sequence into a new form.

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 項目的類型。The type of the elements of source.

TResult

selector 所傳回值的型別。The type of the value returned by selector.

參數

source
IEnumerable<TSource>

要對於叫用轉換函式的值序列。A sequence of values to invoke a transform function on.

selector
Func<TSource,TResult>

要套用至每個項目的轉換函式。A transform function to apply to each element.

傳回

IEnumerable<TResult>

IEnumerable<T>,其項目是對 source 之各個項目叫用轉換函式所產生的結果。An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.

例外狀況

sourceselectornullsource or selector is null.

範例

下列程式碼範例示範如何使用 Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) 來投射值序列。The following code example demonstrates how to use Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) to project over a sequence of values.

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

備註

這個方法是使用延後執行所執行。This method is implemented by using deferred execution. 立即傳回值是一個物件,它會儲存執行動作所需的所有資訊。The immediate return value is an object that stores all the information that is required to perform the action. 這個方法所表示的查詢不會執行,直到以 GetEnumerator foreach Visual c # 或 Visual Basic 中的方法來列舉物件為止 For EachThe query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

此投射方法需要轉換函數, selector 以針對來源序列中的每個值產生一個值 sourceThis projection method requires the transform function, selector, to produce one value for each value in the source sequence, source. 如果傳回 selector 本身為集合的值,則會由取用者以手動方式進行個子序列。If selector returns a value that is itself a collection, it is up to the consumer to traverse the subsequences manually. 在這種情況下,您的查詢可能更適合傳回單一的結合值序列。In such a situation, it might be better for your query to return a single coalesced sequence of values. 若要達成此目的,請使用 SelectMany 方法,而不是 SelectTo achieve this, use the SelectMany method instead of Select. 雖然 SelectMany 的運作方式類似,但轉換函式會 Select 傳回集合,然後在傳回之後展開 SelectManyAlthough SelectMany works similarly to Select, it differs in that the transform function returns a collection that is then expanded by SelectMany before it is returned.

在查詢運算式語法中, select (Visual c # ) 或 Select (Visual Basic) 子句會轉譯為的調用 SelectIn query expression syntax, a select (Visual C#) or Select (Visual Basic) clause translates to an invocation of Select.

適用於