Queryable.Select Metoda

Definicja

Projektuje każdy element sekwencji w nowym formularzu.

Przeciążenia

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

Projektuje każdy element sekwencji w nowym formularzu przez dołączenie indeksu elementu.

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

Projektuje każdy element sekwencji w nowym formularzu.

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Projektuje każdy element sekwencji w nowym formularzu przez dołączenie indeksu elementu.

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)

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ wartości zwracanej przez funkcję reprezentowaną przez selector.

Parametry

source
IQueryable<TSource>

Sekwencja wartości do projektu.

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

Funkcja projekcji, która ma być stosowana do każdego elementu.

Zwraca

IQueryable<TResult>

Element IQueryable<T> , którego elementy są wynikiem wywoływania funkcji projekcji dla każdego elementu elementu source.

Wyjątki

source lub selector ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) metody do projekcji przez sekwencję wartości i używać indeksu każdego elementu w projekcji formularza.

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 }

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na wywołaniu selector dla każdego elementu elementu w celu projekcji source go w innej formie.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Projektuje każdy element sekwencji w nowym formularzu.

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)

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ wartości zwracanej przez funkcję reprezentowaną przez selector.

Parametry

source
IQueryable<TSource>

Sekwencja wartości do projektu.

selector
Expression<Func<TSource,TResult>>

Funkcja projekcji, która ma być stosowana do każdego elementu.

Zwraca

IQueryable<TResult>

Element IQueryable<T> , którego elementy są wynikiem wywoływania funkcji projekcji dla każdego elementu elementu source.

Wyjątki

source lub selector ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) metody do projekcji w sekwencji wartości.

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

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na wywołaniu selector dla każdego elementu elementu w celu projekcji source go w innej formie.

Dotyczy