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方法, 或者通过在foreach视觉C#对象中For Each或在 Visual 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为源source序列中的每个值生成一个值。This 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请使用方法Select而不是。To 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方法, 或者通过在foreach视觉C#对象中For Each或在 Visual 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为源source序列中的每个值生成一个值。This 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请使用方法Select而不是。To 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 (视觉C#对象) Select或 (Visual Basic) 子句转换为对的Select调用。In query expression syntax, a select (Visual C#) or Select (Visual Basic) clause translates to an invocation of Select.

另请参阅

适用于