Queryable.Select 方法

定义

将序列中的每个元素投影到新表单。

重载

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

通过合并元素的索引,将序列的每个元素投影到新窗体中。

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

将序列中的每个元素投影到新表单。

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

通过合并元素的索引,将序列的每个元素投影到新窗体中。

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 的元素类型。

TResult

selector 表示的函数返回的值类型。

参数

source
IQueryable<TSource>

一个要投影的值序列。

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

要应用于每个元素的投影函数。

返回

IQueryable<TResult>

一个 IQueryable<T>,其元素为对 source 的每个元素调用投影函数的结果。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 对值序列进行投影,并使用投影形式中每个元素的索引。

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> 类型。 对于这些参数,可以传入 lambda 表达式,它将编译为 Expression<TDelegate>

方法 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 生成一个 , MethodCallExpression 表示将调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 自身作为构造的泛型方法。 然后,MethodCallExpressionCreateQuery(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 的表达式树而发生的查询行为取决于 参数类型的 source 实现。 预期的行为是,它会调用 selector 的每个元素 source ,以将其投影到不同的形式。

适用于

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

将序列中的每个元素投影到新表单。

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 的元素类型。

TResult

selector 表示的函数返回的值类型。

参数

source
IQueryable<TSource>

一个要投影的值序列。

selector
Expression<Func<TSource,TResult>>

要应用于每个元素的投影函数。

返回

IQueryable<TResult>

一个 IQueryable<T>,其元素为对 source 的每个元素调用投影函数的结果。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 对值序列进行投影。

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> 类型。 对于这些参数,可以传入 lambda 表达式,它将编译为 Expression<TDelegate>

方法 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 生成一个 , MethodCallExpression 表示将调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 自身作为构造的泛型方法。 然后,MethodCallExpressionCreateQuery(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 的表达式树而发生的查询行为取决于 参数类型的 source 实现。 预期的行为是,它会调用 selector 的每个元素 source ,以将其投影到不同的形式。

适用于