Enumerable.Select Enumerable.Select Enumerable.Select Enumerable.Select Method

定義

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

多載

Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) 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>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) 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>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>) 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方法直接或透過foreachVisual C# 中或For EachVisual Basic 中。The 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,它不同,因為轉換函式傳回一個集合,然後展開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>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) 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方法直接或透過foreachVisual C# 中或For EachVisual Basic 中。The 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#) 或Select(Visual Basic) 子句會轉譯成的引動過程SelectIn query expression syntax, a select (Visual C#) or Select (Visual Basic) clause translates to an invocation of Select.

另請參閱

適用於