Sdílet prostřednictvím


Queryable.Select Metoda

Definice

Prodá jednotlivé prvky sekvence do nového formuláře.

Přetížení

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

Každý prvek sekvence začlení do nového formuláře zahrnutím indexu elementu.

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

Prodá jednotlivé prvky 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
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Každý prvek sekvence začlení do nového formuláře zahrnutí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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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 prvků .source

TResult

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

Parametry

source
IQueryable<TSource>

Posloupnost hodnot pro projekt.

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

Projekční funkce, která se použije pro každý prvek.

Návraty

IQueryable<TResult>

Jejíž IQueryable<T> prvky jsou výsledkem vyvolání projekční funkce na každém prvku source.

Atributy

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 projektování přes posloupnost hodnot a použít index každého prvku v projektované podobě.

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 MethodCallExpression , která představuje samotné volání Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderCreateQuery(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, 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 každý prvek source projektu do jiného formuláře.

Platí pro

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

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

Prodá jednotlivé prvky 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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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 prvků .source

TResult

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

Parametry

source
IQueryable<TSource>

Posloupnost hodnot pro projekt.

selector
Expression<Func<TSource,TResult>>

Projekční funkce, která se použije pro každý prvek.

Návraty

IQueryable<TResult>

Jejíž IQueryable<T> prvky jsou výsledkem vyvolání projekční funkce na každém prvku source.

Atributy

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 projektování přes posloupnost 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 MethodCallExpression , která představuje samotné volání Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderCreateQuery(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, 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 každý prvek source projektu do jiného formuláře.

Platí pro