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.
MsgBox(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 方法,或是在 Visual Basic 中使用 Visual C#或 For Each 中的 foreachThe 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.

@No__t-0 的第一個引數代表要處理的元素。The first argument to selector represents the element to process. @No__t-0 的第二個引數代表來源序列中該元素以零為起始的索引。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,但它的差異在於轉換函式會傳回集合,然後在傳回之前由 SelectMany 擴充。Although 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.
MsgBox(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 方法,或是在 Visual Basic 中使用 Visual C#或 For Each 中的 foreachThe 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,但它的差異在於轉換函式會傳回集合,然後在傳回之前由 SelectMany 擴充。Although 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#)或 @no__t 2 (Visual Basic)子句會轉譯為 Select 的調用。In query expression syntax, a select (Visual C#) or Select (Visual Basic) clause translates to an invocation of Select.

另請參閱

適用於