Queryable.Select Queryable.Select Queryable.Select Queryable.Select Method

定義

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

多載

Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 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>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 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>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) 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將傳遞CreateQuery(Expression)至由Provider source參數IQueryProvider的屬性所表示之的方法。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. 預期的行為是它selector會在的source每個專案上叫用, 將其投影至不同的表單。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>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 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將傳遞CreateQuery(Expression)至由Provider source參數IQueryProvider的屬性所表示之的方法。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. 預期的行為是它selector會在的source每個專案上叫用, 將其投影至不同的表單。The expected behavior is that it invokes selector on each element of source to project it into a different form.

適用於