Queryable.Select 方法

定义

将序列中的每个元素投影到新表单。Projects each element of a sequence into a new form.

重载

Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>)

通过合并元素的索引,将序列的每个元素投影到新窗体中。Projects each element of a sequence into a new form by incorporating the element's index.

Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)

将序列中的每个元素投影到新表单。Projects each element of a sequence into a new form.

Select<TSource,TResult>(IQueryable<TSource>, Expression<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::Linq::IQueryable<TResult> ^ Select(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, int, TResult> ^> ^ selector);
public static System.Linq.IQueryable<TResult> Select<TSource,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,TResult>> selector);
static member Select : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, int, 'Result>> -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function Select(Of TSource, TResult) (source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, Integer, TResult))) As IQueryable(Of TResult)

类型参数

TSource

source 的元素类型。The type of the elements of source.

TResult

selector 表示的函数返回的值类型。The type of the value returned by the function represented by selector.

参数

source
IQueryable<TSource>

一个要投影的值序列。A sequence of values to project.

selector
Expression<Func<TSource,Int32,TResult>>

要应用于每个元素的投影函数。A projection function to apply to each element.

返回

IQueryable<TResult>

一个 IQueryable<T>,其元素为对 source 的每个元素调用投影函数的结果。An IQueryable<T> whose elements are the result of invoking a projection function on each element of source.

异常

sourceselectornullsource or selector is null.

示例

下面的代码示例演示如何使用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 来投影值序列并使用投影窗体中每个元素的索引。The following code example demonstrates how to use Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) to project over a sequence of values and use the index of each element in the projected form.

string[] fruits = { "apple", "banana", "mango", "orange", 
                      "passionfruit", "grape" };

// Project an anonymous type that contains the
// index of the string in the source array, and
// a string that contains the same number of characters
// as the string's index in the source array.
var query =
    fruits.AsQueryable()
    .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 }
*/

Dim fruits() As String = {"apple", "banana", "mango", "orange", _
                      "passionfruit", "grape"}

' Project an anonymous type that contains the
' index of the string in the source array, and
' a string that contains the same number of characters
' as the string's index in the source array.
Dim query = _
    fruits.AsQueryable() _
    .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 }

注解

此方法至少具有一个类型 Expression<TDelegate> 的参数,其类型参数为 Func<T,TResult> 类型之一。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 本身。The Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) method generates a MethodCallExpression that represents calling Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderCreateQuery(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) depend on the implementation of the type of the source parameter. 预期的行为是对 source 的每个元素调用 selector,以将其投影到不同的形式。The expected behavior is that it invokes selector on each element of source to project it into a different form.

Select<TSource,TResult>(IQueryable<TSource>, Expression<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::Linq::IQueryable<TResult> ^ Select(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TResult> ^> ^ selector);
public static System.Linq.IQueryable<TResult> Select<TSource,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
static member Select : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Result>> -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function Select(Of TSource, TResult) (source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, TResult))) As IQueryable(Of TResult)

类型参数

TSource

source 的元素类型。The type of the elements of source.

TResult

selector 表示的函数返回的值类型。The type of the value returned by the function represented by selector.

参数

source
IQueryable<TSource>

一个要投影的值序列。A sequence of values to project.

selector
Expression<Func<TSource,TResult>>

要应用于每个元素的投影函数。A projection function to apply to each element.

返回

IQueryable<TResult>

一个 IQueryable<T>,其元素为对 source 的每个元素调用投影函数的结果。An IQueryable<T> whose elements are the result of invoking a projection function on each element of source.

异常

sourceselectornullsource or selector is null.

示例

下面的代码示例演示如何使用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 来投影值序列。The following code example demonstrates how to use Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) to project over a sequence of values.

List<int> range =
    new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Project the square of each int value.
IEnumerable<int> squares =
    range.AsQueryable().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
*/

Dim range As New List(Of Integer)(New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})

' Project the square of each int value.
Dim squares As IEnumerable(Of Integer) = _
    range.AsQueryable().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

注解

此方法至少具有一个类型 Expression<TDelegate> 的参数,其类型参数为 Func<T,TResult> 类型之一。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 本身。The Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) method generates a MethodCallExpression that represents calling Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderCreateQuery(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) depends on the implementation of the type of the source parameter. 预期的行为是对 source 的每个元素调用 selector,以将其投影到不同的形式。The expected behavior is that it invokes selector on each element of source to project it into a different form.

适用于