Queryable.Select Metoda

Definice

Promítá každý prvek sekvence do nového formuláře.

Přetížení

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

Promítá každý prvek sekvence do nové podoby začleněním indexu elementu.

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

Promítá každý prvek sekvence do nového formuláře.

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Promítá každý prvek sekvence do nové podoby začleněním indexu 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ů .source

TResult

Typ hodnoty vrácené funkcí reprezentovanou selectorfunkcí .

Parametry

source
IQueryable<TSource>

Posloupnost hodnot, které chcete promítnout.

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

Funkce projekce, která se má použít pro každý prvek.

Návraty

IQueryable<TResult>

Objekt IQueryable<T> , jehož prvky jsou výsledkem vyvolání funkce projekce pro každý prvek objektu source.

Výjimky

source nebo selector je null.

Příklady

Následující příklad kódu ukazuje, jak použít Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) k promítání na sekvenci hodnot a použití indexu každého prvku v projektovém formuláři.

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 }

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda a bude zkompilován do Expression<TDelegate>.

Metoda Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) vygeneruje metodu MethodCallExpression , která představuje volání Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) sebe sama jako konstruovanou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderCreateQuery(Expression) reprezentované Provider vlastností parametru source .

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazů, který představuje volání Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) , závisí na implementaci typu parametru source . Očekávané chování spočívá v tom, že vyvolá selector na každém prvku pro source promítání do jiné podoby.

Platí pro

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Promítá každý prvek sekvence do nového formuláře.

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ů .source

TResult

Typ hodnoty vrácené funkcí reprezentovanou selectorfunkcí .

Parametry

source
IQueryable<TSource>

Posloupnost hodnot, které chcete promítnout.

selector
Expression<Func<TSource,TResult>>

Funkce projekce, která se má použít pro každý prvek.

Návraty

IQueryable<TResult>

Objekt IQueryable<T> , jehož prvky jsou výsledkem vyvolání funkce projekce pro každý prvek objektu source.

Výjimky

source nebo selector je null.

Příklady

Následující příklad kódu ukazuje, jak použít Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) k promítání na sekvenci hodnot.

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

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda a bude zkompilován do Expression<TDelegate>.

Metoda Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) vygeneruje metodu MethodCallExpression , která představuje volání Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) sebe sama jako konstruovanou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderCreateQuery(Expression) reprezentované Provider vlastností parametru source .

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazů, který představuje volání Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) , závisí na implementaci typu parametru source . Očekávané chování spočívá v tom, že vyvolá selector na každém prvku pro source promítání do jiné podoby.

Platí pro